2016-11-13 110 views
1

試圖測試YoutubeAPI的搜索功能,但得到這個 enter image description here的Youtube API訪問被授權,但我仍然有憑證無效的錯誤

難道是因爲我的頻道(這是綁定到我的Gmail帳戶中,這我目前在console.developers.google中使用)被禁止?

UPD: 創建新帳號,情況還是一樣

那麼,我在這裏做:

  1. 創建,項目在console.developers.google
  2. 激活YouTube數據API(應用程序選用或somth是這樣的,不是 js one),下載了json,看起來像那樣

enter image description here

  • 首先我稱之爲Authorize方法(新頁面顯示,徵求許可,在這裏我只是點擊allow按鈕,一切看起來OK),但後來我試圖用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文件看起來像 enter image description here

    +0

    使用谷歌開發者控制檯帳戶沒有任何關係的OAuth2中使用的帳戶。它是否隨網頁彈出來驗證應用程序?你是401嗎? – DaImTo

    +0

    @DaIm對,我在創建新的YouTube服務後不久就有了新的頁面。 Everthing似乎沒問題,但是當我嘗試使用'serach'時,我得到了這個401錯誤 – DanilGholtsman

    回答

    0

    好吧,我決定一切從頭開始,所以,我不知道究竟是什麼幫助了我,但這裏是我如何使它運行的一個簡單的說明。而且,這裏的證明:) enter image description here

    所以,首先我創建新的項目,它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"); 
           } 
    
          } 
         } 
    
        } 
    } 
    
    相關問題