C# youtube通过C添加订阅#
我正在为windows 8.1构建一个YouTube应用程序 我遇到问题,添加(插入)订阅失败 我的代码:C# youtube通过C添加订阅#,c#,youtube-api,google-oauth,google-api-dotnet-client,C#,Youtube Api,Google Oauth,Google Api Dotnet Client,我正在为windows 8.1构建一个YouTube应用程序 我遇到问题,添加(插入)订阅失败 我的代码: var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( new Uri("ms-appx:///Assets/client_secrets.json"), new[] { Uri.EscapeUriString(YouTubeService.Sco
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new Uri("ms-appx:///Assets/client_secrets.json"),
new[] { Uri.EscapeUriString(YouTubeService.Scope.Youtube) },
"user",
CancellationToken.None);
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
HttpClientInitializer = credential,
ApplicationName = "AppName"
});
Subscription body = new Subscription();
body.Snippet = new SubscriptionSnippet();
body.Snippet.ChannelId = "UC-kezFAw46x-9ctBUqVe86Q";
try
{
var addSubscriptionRequest = youtubeService.Subscriptions.Insert(body, "snippet");
var addSubscriptionResponse = await addSubscriptionRequest.ExecuteAsync();
}
catch (Exception e)
{
throw e;
}
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new Uri("ms-appx:///Assets/client_secrets.json"),
new[] { Uri.EscapeUriString(YouTubeService.Scope.Youtube) },
"user",
CancellationToken.None);
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
//ApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
HttpClientInitializer = credential,
ApplicationName = "4GameTV"
});
try
{
Subscription body = new Subscription();
body.Snippet = new SubscriptionSnippet();
body.Snippet.ResourceId = new ResourceId();
body.Snippet.ResourceId.ChannelId = "UC-kezFAw46x-9ctBUqVe86Q"; //replace with specified channel id
var addSubscriptionRequest = youtubeService.Subscriptions.Insert(body, "snippet");
var addSubscriptionResponse = await addSubscriptionRequest.ExecuteAsync();
}
catch (Exception e)
{
throw e;
}
当我在第一行设置断点时
当执行到最后一行时,中断此函数
更新(2015-11-14):
错误消息:
请求中指定的订阅资源必须使用snippet.resorceId
属性来标识订阅的频道[400]
成功代码:
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new Uri("ms-appx:///Assets/client_secrets.json"),
new[] { Uri.EscapeUriString(YouTubeService.Scope.Youtube) },
"user",
CancellationToken.None);
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
HttpClientInitializer = credential,
ApplicationName = "AppName"
});
Subscription body = new Subscription();
body.Snippet = new SubscriptionSnippet();
body.Snippet.ChannelId = "UC-kezFAw46x-9ctBUqVe86Q";
try
{
var addSubscriptionRequest = youtubeService.Subscriptions.Insert(body, "snippet");
var addSubscriptionResponse = await addSubscriptionRequest.ExecuteAsync();
}
catch (Exception e)
{
throw e;
}
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new Uri("ms-appx:///Assets/client_secrets.json"),
new[] { Uri.EscapeUriString(YouTubeService.Scope.Youtube) },
"user",
CancellationToken.None);
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
//ApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
HttpClientInitializer = credential,
ApplicationName = "4GameTV"
});
try
{
Subscription body = new Subscription();
body.Snippet = new SubscriptionSnippet();
body.Snippet.ResourceId = new ResourceId();
body.Snippet.ResourceId.ChannelId = "UC-kezFAw46x-9ctBUqVe86Q"; //replace with specified channel id
var addSubscriptionRequest = youtubeService.Subscriptions.Insert(body, "snippet");
var addSubscriptionResponse = await addSubscriptionRequest.ExecuteAsync();
}
catch (Exception e)
{
throw e;
}
您的身份验证似乎与我通常使用的有点不同。此外,仅当您希望访问公共数据时才需要ApiKey
string[] scopes = new string[] { YouTubeService.Scope.Youtube };
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, "test"
, CancellationToken.None
, new FileDataStore("Daimto.YouTube.Auth.Store")).Result;
YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "YouTube Data API Sample",
});
您的身份验证似乎与我通常使用的有点不同。此外,仅当您希望访问公共数据时才需要ApiKey
string[] scopes = new string[] { YouTubeService.Scope.Youtube };
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, "test"
, CancellationToken.None
, new FileDataStore("Daimto.YouTube.Auth.Store")).Result;
YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "YouTube Data API Sample",
});
什么是文件数据存储?这是实现IDataStore的类文件数据存储。此存储为类型和键的每个组合创建不同的文件。此文件数据存储存储指定对象的JSON格式。它会保存您的身份验证,因此不会要求您再次进行身份验证。这是刷新令牌的所在地。听起来很难=(,你能提供一些网页,让我能理解你说的话吗,谢谢。我不久前写了一篇关于它的教程,示例是针对Google drive的,但你应该知道。thx供你回复。我已经检查了该网站。它似乎教我如何恢复、刷新令牌。我的令牌永久存在,所以我认为这不是问题。我这样说是因为我的原始代码用于[删除订阅]函数。或其他代码有问题?什么是FileDataStore?这是实现IDataStore的类?文件数据存储区。此存储区为类型和密钥的每个组合创建不同的文件。此文件数据存储区存储指定对象的JSON格式。它保存您的身份验证,因此不会要求您再次进行身份验证。其刷新令牌将在这里生效。这听起来很难=(,你能提供一些网页,让我能理解你说的话吗,谢谢。我不久前写了一篇关于它的教程,示例是针对Google drive的,但你应该知道。thx供你回复。我已经检查了该网站。它似乎教我如何恢复、刷新令牌。我的令牌永久存在,所以我认为这不是问题。我之所以这样说,是因为我的原始代码使用[删除订阅]功能。还是其他代码有问题?