Below describes the only way I was able to get (programmatic) access to the YouTube Analytics API on behalf of our Brand Account. Google documentation is convoluted, to say the least, so if you know a more straightforward way, please do share.
- Create a Project in the Developers Console - https://console.developers.google.com/
- Go to the
Library
tab and enable desired APIs (e.g. YouTube Analytics) - Go to
OAuth consent screen
and make projectexternal
. SelectTest mode
- Go to
Credentials
and createOAtuh Client ID
credential:Application type
= web; redirect uri = https://developers.google.com/oauthplayground - Go to https://developers.google.com/oauthplayground
- Select gear icon ->
Access type
= Offline,Force prompt
= Select Account Screen,Use your own OAuth credentials
= checked --> enterclient id
andclient secret
from step 4 - Click
Select & authorize API's
and select the desired APIs (e.g. https://www.googleapis.com/auth/youtube.readonly , https://www.googleapis.com/auth/youtubepartner , https://www.googleapis.com/auth/yt-analytics-monetary.readonly and https://www.googleapis.com/auth/yt-analytics.readonly - Click 'Authorize APIs'
- In the consent screen, first select your Google account, then in the next screen select your brand account
- Back in the OAuth Playground, you received an Authorization Code. Click
Exchange authorization code for tokens
- Copy the Refresh token
- Go back to your project in the Developers Console and change the User type to
internal
by clicking theMake internal
button - Download your Client ID json from the Developers Console:
Credentials
->OAuth 2.0 Client IDs
table -> Click the download icon next to your Client Id (in the far right of that row)
public async Task<QueryResponse> GetResponse()
{
var credential = GetUserCredential("<YOUR_ClientIdJson>", "<YOUR_RefreshToken>");
QueryResponse response;
using (var youTubeAnalyticsService = new YouTubeAnalyticsService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name,
}))
{
var request = GetRequest(youTubeAnalyticsService);
response = await request.ExecuteAsync();
}
}
private UserCredential GetUserCredential(string clientIdJson, string refreshToken)
{
var stream = GetClientIdStream(clientIdJson);
var token = new TokenResponse { RefreshToken = refreshToken };
return new UserCredential(
new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = GoogleClientSecrets.Load(stream).Secrets
}),
"user",
token
);
}
private static MemoryStream GetClientIdStream(string clientIdJson)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(clientIdJson);
writer.Flush();
stream.Position = 0;
return stream;
}
I have myself as a test user already. I get a 403 error every time I query with my channel id. Authenticating as either the brand account or test user/brand account owner