【发布时间】:2017-03-27 04:51:03
【问题描述】:
可能是因为我的频道(绑定到我的 gmail 帐户,我目前在 console.developers.google 中使用)被禁止了吗?
更新: 创建了新帐户,情况还是一样
好吧,我在这里做了什么:
- 在
console.developers.google中创建了项目 - 已激活 youtube 数据 api(选择的应用程序或类似的应用程序,而不是 js一),下载的json,看起来是这样的
- 首先我调用
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>();
}
}
}
【问题讨论】:
-
使用google开发者控制台的账号与OAuth2中使用的账号无关。它是否弹出网页以验证应用程序?你得到 401 了吗?
-
@DaImTo 是的,我在创建新的
YouTubeService后不久就获得了新页面。一切似乎都还好,但是当我尝试使用serach时,我遇到了这个 401 错误
标签: c# oauth-2.0 google-api youtube-api access-token