Youtube API 访问权限已授予,但我仍然遇到无效凭据错误

Youtube API access is granted but I still have Invalid Credentials error

正在尝试测试 YoutubeAPI 的搜索功能,但得到了这个

会不会是因为我的频道(绑定到我的gmail帐户,我目前在console.developers.google中使用)被禁止了?

更新: 创建了新帐户,情况还是一样

好吧,我在这里做了什么:

  1. console.developers.google
  2. 中创建了项目
  3. 激活了 youtube 数据 api(选择了这样的应用程序,而不是 js 一个),已下载 json,看起来像

  1. 首先我调用 Authorize 方法(新页面显示,请求许可,我只是点击 allow 按钮,一切似乎都正常),但随后我尝试使用 Search 我收到 401 错误

代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication3.Services.ServiceAuthoriaztion.Impl
{
    using System.Data.Entity.Core.Metadata.Edm;
    using System.IO;
    using System.Threading;
    using Google.Apis;
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Services;
    using Google.Apis.Util.Store;
    using Google.Apis.YouTube.v3;


    public class Youtube : ICustomService
    {
        private static YouTubeService _currentYouTubeService;

        public void Authorize()
        {
            UserCredential userCreds;
           ;
            var filePath = string.Format("{0}{1}",AppDomain.CurrentDomain.BaseDirectory, @"App_Data\client_id.json");
            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                userCreds = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] {YouTubeService.Scope.YoutubeReadonly},
                    "user",
                    CancellationToken.None,
                    new FileDataStore("YouTubeData")
                    ).Result;
            }

            _currentYouTubeService = new YouTubeService(new BaseClientService.Initializer
            {
                HttpClientInitializer = userCreds,
                ApplicationName = "yttest"
            });

            SerachTest();

        }

        private void SerachTest()
        {
            var searchListRequest = _currentYouTubeService.Search.List("snippet");
            searchListRequest.Q = "Google"; // Replace with your search term.
            searchListRequest.MaxResults = 50;

            // Call the search.list method to retrieve results matching the specified query term.
            var searchListResponse = searchListRequest.Execute();

            var asa = new List<string>();

        }
    }
}

UPD2: 尝试了其他类型的应用程序 - 也没有帮助。 JSON 文件看起来像这样

好吧,我决定一切从头开始,所以,我不知道到底是什么帮助了我,但这里有一个关于我如何让它工作的简单说明。而且,这是证明:)

所以,首先我创建了一个新项目,并为其设置了名称DanilTestApp

其次,打开YouTube Data API那里。

当我创建新的凭据时,选择了 OAuth Client ID,然后,作为类型,选择了 Other

准备好后我就下载了 JSON。

然后在我的代码中我决定添加刷新令牌功能,所以现在它看起来像这样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication3.Services.ServiceAuthoriaztion.Impl
{
    using System.Data.Entity.Core.Metadata.Edm;
    using System.IO;
    using System.Threading;
    using Google.Apis;
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Services;
    using Google.Apis.Util.Store;
    using Google.Apis.YouTube.v3;


    public class Youtube : ICustomService
    {
        private static YouTubeService _currentYouTubeService;

        public void Authorize()
        {
            UserCredential userCreds; 
            var filePath = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, @ "App_Data\client_id.json");
            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                userCreds = GoogleWebAuthorizationBroker.AuthorizeAsync(
                 GoogleClientSecrets.Load(stream).Secrets,
                 new[] {
          YouTubeService.Scope.YoutubeReadonly
                 },
                 "user",
                 CancellationToken.None,
                 new FileDataStore("YouTubeData")
                ).Result;
            }

            RefreshToken(userCreds);

            _currentYouTubeService = new YouTubeService(new BaseClientService.Initializer
            {
                HttpClientInitializer = userCreds,
                ApplicationName = "DanilTestApp"
            });

            SerachTest();

        }

        private void SerachTest()
        {
            var searchListRequest = _currentYouTubeService.Search.List("snippet");
            searchListRequest.Q = "Google"; // Replace with your search term.
            searchListRequest.MaxResults = 50;

            // Call the search.list method to retrieve results matching the specified query term.
            var searchListResponse = searchListRequest.Execute();

            var asa = new List<string>();

        }
        //might not work, but something like this, got working code at home, office right now
        private void UpdateTokenIfExpired(UserCredential credential)
        {
           if (credential.Token.IsExpired(credential.Flow.Clock))
           {

               Console.WriteLine("The access token has expired, refreshing it");
               if (credential.RefreshTokenAsync(CancellationToken.None).Result)
               {
                   Console.WriteLine("The access token is now refreshed");
               }
               else
               {
                   throw new Exception("refresh token failed");
               }

           }
       }

    }
}