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:
- Go to https://console.developers.google.com/ for your main google account.
- In the sidebar, go to APIs & Services, then Credentials
- At the top, click +Create Credentials, then Service account
- Under Service account details, enter any name, then click Create and Continue
- Under "Grand this service account access to project", click continue
- Under "Grant users access to this service account", click Done
- On the main credentials page that loads, click the newly created service account under Service Accounts
- In the tabs, click Keys
- Click the Add Key button, then Create new key
- Keep JSON, then click create
- Save the file as client-key.json in the root of your nodejs project
- Go to https://developers.google.com/oauthplayground
- 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
.
- In the window that pops up, click your youtube (brand) account, then allow
- In the next step, click Exchange authorization code for tokens
- Copy the access token
- 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.