I'm writing a simple application that takes some new uploads from subscribed channels and adds them to a specified playlist. Service account type is used with following code:
var certificate = new X509Certificate2(certificateFileName, "notasecret", X509KeyStorageFlags.Exportable);
var serviceInitializer = new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { YouTubeService.Scope.Youtube }
};
var serviceInitializerInstance = serviceInitializer.FromCertificate(certificate);
var credential = new ServiceAccountCredential(serviceInitializerInstance);
var youtubeService = new YouTubeService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = applicationName
});
I've succeeded in reading all necessary information but when I try to insert item into playlist it always return this error:
Error: Google.Apis.Requests.RequestError Access forbidden. The request may not be properly authorized. [403]
Errors [ Message[Access forbidden. The request may not be properly authorized.] Location[ - ] Reason[forbidden] Domain[yo utube.common] ]
It seems to be some Core API general error according to this link.
I've seen a lot of samples and they are all the same. My code is:
var newPlaylistItemClone = new PlaylistItem();
newPlaylistItem.Snippet = new PlaylistItemSnippet();
newPlaylistItem.Snippet.PlaylistId = playlist.Id;
newPlaylistItem.Snippet.ResourceId = new ResourceId();
newPlaylistItem.Snippet.ResourceId.VideoId = existingPlaylistItem.Snippet.ResourceId.VideoId;
newPlaylistItem.Snippet.ResourceId.Kind = existingPlaylistItem.Snippet.ResourceId.Kind;
await youTubeService.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync();
Where newPlaylistItem
is an item I want to insert and existingPlaylistItem
is existing item from channel uploads playlist. I just copy the values and change the playlist id.
Also, when using API explorer on Google Developers documentation website everything works fine (Web Authorization is used). In Google Developers Console I have all the API's enabled. Maybe the issue is that I'm lacking some info required for service account type? Can anyone help me to resolve this issue?
Here's my GitHub repository with source code. Error occurs on line 235 on request.