0
votes

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.

2

2 Answers

2
votes

Quoting from official Google Youtube API documentation:

Service accounts do not work for YouTube Data API calls because service accounts require an associated YouTube channel, and you cannot associate new or existing channels with service accounts. If you use a service account to call the YouTube Data API, the API server returns an error with the error type set to unauthorized and the reason set to youtubeSignupRequired.

Please refer.

0
votes

Most easy way of doing this is going to your consent screen of your application. Scopes

Edit your app by adding the YouTube scope(you can add more scopes if you desire).Re log in and test the playlist function again

Scopes

Worked for me and i managed to obtain the playlists of the account trough the YouTubeService class