Get comments thread for a YouTube video using api v3.0

六眼飞鱼酱① 提交于 2019-12-24 03:53:06

问题


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();

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


回答1:


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

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.

Code to revoke token:

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.



来源:https://stackoverflow.com/questions/30887427/get-comments-thread-for-a-youtube-video-using-api-v3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!