YouTube APIs - Access mutiple youtube channels (Brand Accounts) using Google Admin account

送分小仙女□ 提交于 2019-12-17 10:55:16

问题


I need to extract information from videos using YouTube Analytics and Reporting Api.

I have access to multiple YouTube Brand Accounts, when I log into YouTube with my Google Account.

Using the "Try it" for testing the API, I'm only able to retrieve data for a channel once I switch to the Brand Account that this channel belongs, otherwise I get 403 - Forbidden error.

Is there any way to extract data using the Google Account that I'm using to log in? Because once I create the credentials in developers console, they will be associated to the Google Account and not to the Brand Accounts.

My google account has Manager Role on the brand accounts.

I've search for the onBehalfOfContentOwner field to be used in requests, but I don't know how to get this ID, and I'm not sure if this is applicable in my situations, since we're talking about Brand Accounts, correct me if I'm wrong.


回答1:


I fought with this just two days ago. Turns out it IS possible, it's just undocumented and works a bit differently than you'd expect:

Once I create the credentials in developers console, they will be associated to the Google Account and not to the Brand Accounts.

I had the same exact misconception when I first tried (even went so far as to find out the brand account's client_id). Turns out you don't want to use the brand's oauth info -- you want to use your own client_id/client_secret to create a refresh token on behalf of the brand account then use that to create auth tokens.

Steps:

  1. Using your main account create an oauth client_id and client_secret via https://console.developers.google.com/apis/credentials
  2. Edit the client_id/client_secret entry you just added and add "https://developers.google.com/oauthplayground" to the "Authorized redirect URIs" at the bottom of the page.
  3. We're going to create a refresh token the lazy way. Go to https://developers.google.com/oauthplayground/
  4. Click the gears on the top right corner and set access type to "offline", then click "Use your own OAuth credentials" and enter the client_id and client_secret you created in step 1.
  5. Select the scopes you want to give it access to. Click authorize APIs.
  6. Here's the magic bit: You'll now be asked to "Choose an account". Choose the brand account you want to access here, NOT your main account. Since you have permission to access it this'll work fine even though you're using your own client_id and client_secret
  7. Allow the permission access when it prompts you, then you'll be brought back to the oauth playground.
  8. Click "Exchange authorization code for tokens"
  9. Grab the refresh token and use it like normal to generate auth tokens as needed.

Congratulations, you now have api access to the brand account!

Hope that helps.




回答2:


The YouTube API is different then other google APIs. With other APIs you authenticate access to the full account. However with the YouTube API its channel based. You are going to need to authenticate your application once for each channel.

onBehalfOfContentOwner

This parameter is intended for YouTube content partners that own and manage many different YouTube channels. It allows content owners to authenticate once and get access to all their video and channel data, without having to provide authentication credentials for each individual channel. The actual CMS account that the user authenticates with needs to be linked to the specified YouTube content owner.

You need to be a YouTube partner then you can contact your account manager and get a CMS id. I have yet to figure out what magic one must archive to become a YouTube partner.




回答3:


If you follow the solution for getting a permanent refresh token and use Java, this works for me

GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                    .setJsonFactory(JSON_FACTORY)
                    .setClientSecrets(oauth2ClientId, oauth2ClientSecret)
                    .build()
                    .setRefreshToken(oauth2RefreshToken);


 this.youTubeClient = new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
                    .setApplicationName(APPLICATION_NAME)
                    .build();

Required dependencies

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-youtube</artifactId>
    <version>v3-rev212-1.25.0</version>
    </dependency>
<dependency>
    <groupId>com.google.auth</groupId>
    <artifactId>google-auth-library-oauth2-http</artifactId>
    <version>0.18.0</version>
</dependency>

These resources might also help once you have the refresh token:

  • Authenticate programmatically to Google with OAuth2
  • https://blog.timekit.io/google-oauth-invalid-grant-nightmare-and-how-to-fix-it-9f4efaf1da35


来源:https://stackoverflow.com/questions/41016537/youtube-apis-access-mutiple-youtube-channels-brand-accounts-using-google-adm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!