【发布时间】:2017-11-19 11:43:34
【问题描述】:
我使用C# 创建了console 应用程序。
它将upload Video 从本地驱动器到youtube。
我使用this link 在google api 中创建了新应用程序。
我还使用nuget 安装了所有必需的packages。
当我运行我的应用程序时,出现“Access Denied”错误,我无法找到问题。
Task Run() 方法出现错误。
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
namespace Google.Apis.YouTube.Samples
{
/// <summary>
/// YouTube Data API v3 sample: create a playlist.
/// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
/// See https://developers.google.com/api-client-library/dotnet/get_started
/// </summary>
internal class PlaylistUpdates
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("YouTube Data API: Playlist Updates");
Console.WriteLine("==================================");
try
{
new PlaylistUpdates().Run().Wait();
}
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 Run()
{
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()
});
// Create a new, private playlist in the authorized user's channel.
var newPlaylist = new Playlist();
newPlaylist.Snippet = new PlaylistSnippet();
newPlaylist.Snippet.Title = "Test Playlist";
newPlaylist.Snippet.Description = "A playlist created with the YouTube API v3";
newPlaylist.Status = new PlaylistStatus();
newPlaylist.Status.PrivacyStatus = "public";
newPlaylist = await youtubeService.Playlists.Insert(newPlaylist, "snippet,status").ExecuteAsync();
// Add a video to the newly created playlist.
var newPlaylistItem = new PlaylistItem();
newPlaylistItem.Snippet = new PlaylistItemSnippet();
newPlaylistItem.Snippet.PlaylistId = newPlaylist.Id;
newPlaylistItem.Snippet.ResourceId = new ResourceId();
newPlaylistItem.Snippet.ResourceId.Kind = "youtube#video";
newPlaylistItem.Snippet.ResourceId.VideoId = "GNRMeaz6QRI";
newPlaylistItem = await youtubeService.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync();
Console.WriteLine("Playlist item id {0} was added to playlist id {1}.", newPlaylistItem.Id, newPlaylist.Id);
}
}
}
我是否需要将 'user' 参数传递给 gmail 用户名?
任何使用 C#(控制台/Web)的工作示例?
帮助表示赞赏。
【问题讨论】:
-
@Chad 那么到底有什么问题呢?失败在哪里?相同的“拒绝访问”?
-
我收到
Inner Exception 1: TokenResponseException: Error:"invalid_client", Description:"Unauthorized", Uri:""错误,请参阅我的帖子stackoverflow.com/questions/49364545/… 我一遍又一遍地看到相同的代码示例和问题,但没有答案。我想知道 .NET 库是否可以正常工作。我已经用了一个多星期了。 -
@Chad 我刚刚尝试过,对我来说效果很好(不是所有的东西,只是授权部分)。所以这就是为什么我要问你到底哪里有错误。在哪条线上?授权还是以后?
-
在
Task Run()我的代码执行到credential = await GoogleWebAuthorizationBroker.AuthorizeAsync()的行/方法 -
@Chad 您的错误表明您的客户端 id\secret 存在一些问题。尝试直接初始化它们:
AuthorizeAsync(new ClientSecrets {ClientId = "your id", ClientSecret = "secret"}...)。使用此页面中的 OAuth2.0 凭据:console.developers.google.com/apis/credentials。修复该问题后 - 运行代码时应打开 Web 浏览器页面。您应该在浏览器中授权此应用程序(登录到 gmail 帐户等)。之后它应该可以正常工作了。
标签: c# google-api youtube-data-api google-api-dotnet-client google-data-api