C#,YouTube数据API,CommentThreads.List(),使用OAuth时身份验证作用域不足
如果我使用API键方法(下面的C#,YouTube数据API,CommentThreads.List(),使用OAuth时身份验证作用域不足,c#,youtube-data-api,C#,Youtube Data Api,如果我使用API键方法(下面的RunAPIKey()方法),它会成功。但是如果我使用OAuth方法(RunOauth()),它会失败,出现以下异常: Error: Google.Apis.Requests.RequestError Insufficient Permission: Request had insufficient authentication scopes. [403] Errors [ Message[Insufficient Permission: Reque
RunAPIKey()
方法),它会成功。但是如果我使用OAuth方法(RunOauth()
),它会失败,出现以下异常:
Error: Google.Apis.Requests.RequestError
Insufficient Permission: Request had insufficient authentication scopes. [403]
Errors [
Message[Insufficient Permission: Request had insufficient authentication scopes.] Location[ - ] Reason[insufficientPermissions] Domain[global]
]
为什么会这样?当然,当浏览器打开OAuth页面时,我已经允许我的YouTube帐户使用它。我发现了一个类似的问题,但是添加“forcessl”范围的答案不起作用。用API键设置req.Key
也不起作用。我用过
解决方案实际上是添加了
YouTubeService.Scope.YoutubeForceSsl
Scope。这是愚蠢的,因为它没有提到它需要YoutubeForceSsl
,这与insert
API不同
但是,仅仅在已经获得身份验证之后添加它是不起作用的。它必须再次获得身份验证才能应用更改的范围。因为我不知道怎么做,我打开了另一个
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
namespace APITest
{
internal class Test
{
static void Main(string[] args)
{
Console.WriteLine("YouTube Data API");
Console.WriteLine("========================");
try
{
//new Test().RunAPIKey().Wait(); //Works.
new Test().RunOauth().Wait(); //Doesn't work.
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("Error: " + e.Message);
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
private async Task RunAPIKey()
{
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "apparently_I_cant_show_this",
ApplicationName = this.GetType().ToString()
});
var req = youtubeService.CommentThreads.List("snippet");
req.VideoId = "NK7iIBcsBn4";
req.MaxResults = 5;
var res = await req.ExecuteAsync();
foreach (var item in res.Items)
{
Console.WriteLine(item.Snippet.TopLevelComment.Snippet.TextDisplay);
}
}
private async Task RunOauth()
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows for full read/write access to the
// authenticated user's account.
new[] { YouTubeService.Scope.Youtube },
"user",
CancellationToken.None,
new FileDataStore(this.GetType().ToString())
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
var req = youtubeService.CommentThreads.List("snippet");
req.VideoId = "NK7iIBcsBn4";
req.MaxResults = 5;
var res = await req.ExecuteAsync();
foreach (var item in res.Items)
{
Console.WriteLine(item.Snippet.TopLevelComment.Snippet.TextDisplay);
}
}
}
}