bmvr
bmvr

Reputation: 876

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

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.

Upvotes: 18

Views: 11313

Answers (4)

55 Cancri
55 Cancri

Reputation: 1195

I will give an update to @Paolo's incredible answer. In my case, I was trying to get my private videos using the Playlist.list api. I've never seen an api as poorly documented, asinine, and CONVOLUTED as youtube's api.

Context: I have a main google account for which my youtube api credentials are tied to (there is no google developer accounts for youtube brand accounts) but would like to get the private playlists (and videos) for my youtube account (a brand account). mine=true, key, channelId, onBehalfOfContentOwner, and onBehalfOfContentOwnerChannel all did NOTHING for me. I was getting either public playlists or api errors with various combinations and values of those parameters.

In the end, these were the steps I took to run a node script to get private videos from my brand account:

  1. Go to https://console.developers.google.com/ for your main google account.
  2. In the sidebar, go to APIs & Services, then Credentials
  3. At the top, click +Create Credentials, then Service account
  4. Under Service account details, enter any name, then click Create and Continue
  5. Under "Grand this service account access to project", click continue
  6. Under "Grant users access to this service account", click Done
  7. On the main credentials page that loads, click the newly created service account under Service Accounts
  8. In the tabs, click Keys
  9. Click the Add Key button, then Create new key
  10. Keep JSON, then click create
  11. Save the file as client-key.json in the root of your nodejs project
  12. Go to https://developers.google.com/oauthplayground
  13. Scroll to bottom of scopes and select YouTube Data API v3 v3, then https://www.googleapis.com/auth/youtube and https://www.googleapis.com/auth/youtube.readonly.
  14. In the window that pops up, click your youtube (brand) account, then allow
  15. In the next step, click Exchange authorization code for tokens
  16. Copy the access token
  17. Go back to your node script and use like this:
  const auth = new google.auth.GoogleAuth({
    keyFile: "client-key.json",
    scopes: [
      "https://www.googleapis.com/auth/youtube",
      "https://www.googleapis.com/auth/youtube.force-ssl",
      "https://www.googleapis.com/auth/youtube.readonly",
      "https://www.googleapis.com/auth/youtubepartner",
      "https://www.googleapis.com/auth/youtubepartner-channel-audit",
    ],
  })

  const authClient = await auth.getClient()
  google.options({ auth: authClient })

  const youtube = google.youtube("v3")

  const token = "your token here"

  const results = await youtube.playlists.list({
    part: [
      "snippet",
      "id",
      "contentDetails",
      "status",
      "localizations",
      "status",
    ],
    mine: true,
    auth: token,
    oauth_token: token,
    maxResults: 50,
  })

Note mine: true and that the token must be passed to BOTH auth and oauth_token, but not key. If either parameter is missing, the call will fail. (Why? No clue. Please tell me.) Also, you must continuously renew your access token in the playground after it expires.

Now, with all of this said, I encourage you to find me an api worse than the youtube api. My guess is you'll be hard-pressed to find one even half as ridiculous as this.

P.S. I believe there were additional things required before this such as enabling the youtube api and doing something on the OAUTH Consent Screen but I'm too exhausted with this thing to continue. Hopefully the Google console UX will be enough to guide you through those steps, though quite frankly, I doubt it.

Hope this helps and good luck, because you may actually need it.

Upvotes: 2

rieckpil
rieckpil

Reputation: 12051

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:

Upvotes: 0

Paolo
Paolo

Reputation: 440

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.

Upvotes: 25

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117281

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.

Upvotes: 5

Related Questions