Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#,YouTube数据API,CommentThreads.List(),使用OAuth时身份验证作用域不足_C#_Youtube Data Api - Fatal编程技术网

C#,YouTube数据API,CommentThreads.List(),使用OAuth时身份验证作用域不足

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

如果我使用API键方法(下面的
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);
            }
        }
    }
}