使用 api v3.0 获取 YouTube 视频的评论线程 [英] Get comments thread for a YouTube video using api v3.0

查看:29
本文介绍了使用 api v3.0 获取 YouTube 视频的评论线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 .net YouTube API v.3.0 来检索评论线程.我基本上是在尝试重用 Java 示例,但是当我尝试使用我的客户端机密进行授权 我没有使用我的令牌获得任何范围(当我检查凭证变量时它被设置为 null).这是我从 .net 示例中实际获取的代码:

I'm trying to use the .net YouTube API v.3.0 to retrieve the comments thread. I'm basically trying to reuse the sample for Java, but when I try to authorize with my client secrets I do not get any scope with my token (it is set to null when I inspect the credential variable). Here is the code I literally took from .net example:

credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets,
    new[] { YouTubeService.Scope.YoutubeForceSsl},
    "user",
    CancellationToken.None,
    new FileDataStore(this.GetType().ToString())
);

我可以列出我上传的所有视频,但我无法使用以下代码获取它们的评论线程:

I can list all the videos I uploaded, but I cannot get the comments thread for them using the code below:

var req = youtubeService.CommentThreads.List("snippet");
req.VideoId = playlistItem.Snippet.ResourceId.VideoId;
req.TextFormat = CommentThreadsResource.ListRequest.TextFormatEnum.PlainText;
var res = req.Execute();

youtubeService"是通过传递之前创建的凭据"变量创建的.有人在使用 .net 时获得评论的运气更好吗?

The "youtubeService" has been created by passing the "credential" variable created earlier. Anyone had more luck with getting comments using .net?

推荐答案

重现步骤:我不得不为此解决它 - 结果我多次对应用程序进行更改,最初我只要求阅读播放列表(如本例所示:检索我上传的内容).因此我授权了一个范围:

Repro steps: I had to sleep with this to solve it - it turns out that I was making changes to the app multiple times, and initially I only requested to read the playlists (as by this example: Retrieve my uploads). Therefore I authorized with a scope:

YouTubeService.Scope.YoutubeReadonly

这个范围显然不足以读取 CommentThreads,但是当我更改范围时,应用程序保持相同的授权令牌并将其用于我的所有请求(范围设置为只读).

This scope obviously is not enough to read CommentThreads, but when I changed the scope the app kept the same authorization token and used it with all my requests (with the scope set to read-only).

我为解决这个问题做了什么:我从我的应用程序代码中撤销了令牌并再次授权.这一次,因为没有缓存令牌,出现了一个同意窗口,其范围是管理您的帐户"(之前是只读访问"),这次我能够获得一个适当范围的令牌.

What I did to solve this problem: I revoked the token from the code of my app and authorized again. This time, because there was no token cached, a consent window appeared with a scope to "manage your account" (before it was "read-only access"), and I was able to obtain a token with a proper scope this time.

撤销令牌的代码:

await credential.RevokeTokenAsync(CancellationToken.None);

期望:我期待,因为我更改了代码中的范围,同意窗口将再次出现而无需撤销令牌,因为我缓存的令牌具有较低/不同的范围.

Expectations: I was expecting that because I changed the scope in my code, the consent window will appear again without the need to revoke the token, because my cached token had a lower/different scope.

总结:问题(错误还是设计?)是,一旦您在应用中获得令牌并想要更改范围(到更强大的范围),您实际上需要撤销令牌并再次授权,否则您最终会获得具有初始作用域的相同令牌,即使它在代码中已被更改.

Summary: The issue (bug or by design?) is that once you obtain token within your app and want to change the scope (to the more powerful one), you actually need to revoke the token and authorize again, otherwise you will end up getting the same token with initial scope, even though it has been changed in the code.

这篇关于使用 api v3.0 获取 YouTube 视频的评论线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆