1
試圖測試YoutubeAPI的搜索功能,但得到這個 的Youtube API訪問被授權,但我仍然有憑證無效的錯誤
難道是因爲我的頻道(這是綁定到我的Gmail帳戶中,這我目前在console.developers.google中使用)被禁止?
UPD: 創建新帳號,情況還是一樣
那麼,我在這裏做:
- 創建,項目在
console.developers.google
- 激活YouTube數據API(應用程序選用或somth是這樣的,不是 js one),下載了json,看起來像那樣
- 首先我稱之爲
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>();
}
}
}
使用谷歌開發者控制檯帳戶沒有任何關係的OAuth2中使用的帳戶。它是否隨網頁彈出來驗證應用程序?你是401嗎? – DaImTo
@DaIm對,我在創建新的YouTube服務後不久就有了新的頁面。 Everthing似乎沒問題,但是當我嘗試使用'serach'時,我得到了這個401錯誤 – DanilGholtsman