3
votes

I am trying to use Google Service Account to list videos from YouTube Data API v3 in .NET. I have activated YouTube API, created Account Service, but whenever I am trying to list videos from my channel, it says 0 videos even if I do have uploaded videos. Could you please help ? Thanks.

Here is the code -

string[] scopes = new string[] 
{    
    YouTubeAnalyticsService.Scope.YtAnalyticsReadonly, 
    YouTubeAnalyticsService.Scope.YtAnalyticsMonetaryReadonly, 
    "https://www.googleapis.com/auth/youtube.force-ssl" 
};

// service account credential
GoogleCredential credential = GoogleCredential.FromFile(SecretPath).CreateScoped(scopes);

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = channelinfo.AnalyticsAPIFileDataStore
});

Dictionary<string, DateTime?> videoIds = new Dictionary<string, DateTime?>();

var channelResourcesList = youtubeService.Channels.List("contentDetails");
channelResourcesList.Id = channelinfo.ChannelId;
var listResult = channelResourcesList.Execute();
string playListId = listResult.Items[0]?.ContentDetails.RelatedPlaylists.Uploads;

var listOfVideos = youtubeService.PlaylistItems.List("snippet");

listOfVideos.PlaylistId = playListId;
var nextPageToken = "";

while (nextPageToken != null)
{
    listOfVideos.MaxResults = 50;
    listOfVideos.PageToken = nextPageToken;

    // searchresult is empty (listOfVideos.Execute() returns empty result) although channelResourcesList.Execute() worked
    var searchListResult = listOfVideos.Execute();
    nextPageToken = searchListResult.NextPageToken;
}
1
Thank you for looking into the issue. I have updated the code.Anindita Ghatak
AFAIK, Youtube API doesn't support service account because it is not associated to any YouTube channel, and you cannot associate new or existing channels with service accounts.Mr.Rebot
In youtube API wiki - developers.google.com/youtube/registering_an_application it's mentioned "You can generate OAuth 2.0 credentials for web applications, service accounts, or installed applications."Anindita Ghatak

1 Answers

2
votes

The YouTube API doesn't support services account authentication.

There is an error in the page you have linked if you notice they don't say how to set up a service account that is because this is most likely generated documentation. I have contacted the team with luck they will fix it so that no other developers will be confused.

You will need to use Oauth2 and authenticate your user at least once then save the refresh token an use that to request a new access token when ever you need to access the API.