Reputation: 31
I am getting below error, I wanted to get all the comments posted on a youtube video.
So basically I am passing video id and I wanted to get all the comments associated with that video
Google.Apis.Requests.RequestError
Insufficient Permission [403]Errors [Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]]
Here is my code:
protected void btnGetVideoDesc_Click(object sender, EventArgs e)
{
string videoId = txtVideoID.Text;
YoutubeVideo video = new YoutubeVideo(videoId);
lblTitle.Text = video.title;
lblPublishedDate.Text = video.publishdate.ToShortDateString();
}
public class YoutubeVideo
{
public string id, title, description ;
public DateTime publishdate;
public YoutubeVideo(string id)
{
this.id = id;
YoutubeAPI.GetVideoInfo(this);
}
}
public class YoutubeAPI
{
private static YouTubeService ytService = Auth();
private static YouTubeService Auth()
{
UserCredential creds;
var service = new YouTubeService();
try
{
using (var stream = new FileStream(@"C:\v-mmarat\Project\EMEA_Development\YoutubeWebCrawling\YoutubeWebCrawling\youtube_client_secret.json", FileMode.Open, FileAccess.Read))
{
creds = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.YoutubeReadonly }, "user", CancellationToken.None,
new FileDataStore("YoutubeAPI")
).Result;
}
service = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = creds,
ApplicationName = "YoutubeAPI",
ApiKey = "My_API_Key"
});
}
catch (Exception e)
{ }
return service;
}
public static void GetVideoInfo(YoutubeVideo video)
{
try
{
//This code work perfectly
var videoRequest = ytService.Videos.List("snippet");
videoRequest.Id = video.id;
var response = videoRequest.Execute();
if (response.Items.Count > 0)
{
video.title = response.Items[0].Snippet.Title;
video.description = response.Items[0].Snippet.Description;
video.publishdate = response.Items[0].Snippet.PublishedAt.Value;
}
else
{
//error
}
var CommentRequest = ytService.Comments.List("snippet");
videoRequest.Id = video.id;
//Getting error at this line after CommentRequest.Execute();
var Commentresponse = CommentRequest.Execute();
if (Commentresponse.Items.Count > 0)
{
video.title = Commentresponse.Items[0].Snippet.ChannelId;
video.description = Commentresponse.Items[0].Snippet.TextDisplay;
video.publishdate = Commentresponse.Items[0].Snippet.PublishedAt.Value;
}
else
{
//error
}
}
catch (Exception e)
{ }
}
Upvotes: 3
Views: 7354
Reputation: 2453
Same here.
Got it solved by specifying a the "dataStore" parameter in "GoogleWebAuthorizationBroker.AuthorizeAsync".
The service than wrote an acces-token JSON file into that folder.
Seems the storage option was required for authorization.
Also when authorization succeded, the browser jumped to an Google-authorisation URL where I had to logon and allow the levels of access I had requested from the API.
I used the API all the time before, but only for readonly ops. Seems the "action" stuff (insert, update, delete, upload) requires more grants.
Upvotes: 0
Reputation: 61
In GoogleWebAuthorizationBroker.AuthorizeAsync
change "user" to "admin".
Upvotes: 5
Reputation: 31
This is a really late response but I had a similar issue. My project was using the YouTube API to upload videos to an account and in a separate section of the code was using it again to search for a video by ID to check its status.
My issue was I was using the same OAuth credentials to upload and then also for searching for a video.
This was failing because I had already set the YouTube Scope when uploading which was not the correct scope for searching for a video.
My simple solution for this was to create another set of OAuth credentials (Of type "Other") via the Google Developer Console, download the json file and use these details to obtain a different access token for the searching part of my code.
Hopefully this helps someone out.
Upvotes: 3
Reputation: 406
I know this answer is a little late, but this is what fixed it for me. Hopefully it helps someone else.
I was receiving the same permissions denied error. After adding the YouTubeService.Scope.YoutubeForceSsl
item to the scopes list, I was able to pull the comments.
Also, it looks like your code is not quite right. You will want to pull the CommentThreads based on the VideoId and include the replies (if you want them).
You won't be able to pull the comments for a video using Comments.
var threadsRequest = Client.CommentThreads.List("snippet,replies");
threadsRequest.VideoId = videoId;
var response = threadsRequest.Execute();
Upvotes: 0