Youtube API v3 : access youtube videos using service account
Asked Answered
P

2

6

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;
}
Pottle answered 4/8, 2018 at 1:20 Comment(3)
Thank you for looking into the issue. I have updated the code.Pottle
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.Jessejessee
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."Pottle
S
3

update

In formation on how to configure a service account with use with the YouTube API can be found here. Server sided web apps

As well as a C# example

GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
                {
                    credential = GoogleCredential.FromStream(stream)
                         .CreateScoped(scopes);
                }

                // Create the  YouTubeService service.
                return new YouTubeService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "YouTube Service account Authentication Sample",
                });
Suppress answered 8/12, 2018 at 23:23 Comment(1)
I'm not sure if this is because it's outdated, but I am using Youtube Data API with Service Accounts as described in my answerHebdomadal
K
0

You can configure a service account for use with Youtube API. However, you will not be able to access any private content. "...you cannot associate new or existing channels with service accounts."

Kenosis answered 14/10, 2022 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.