Allowing Google Service Account YouTube Upload Access via API v3
Asked Answered
T

4

16

I want to automatically upload videos to YouTube without user involvement so I've created a service account, jumped through the hoops, all was looking great, then the upload, chunk one, is attempted and my code bombs with this Google_Exception exception:

"Failed to start the resumable upload (HTTP 401: youtube.header, Unauthorized)"

I then dug and found on the YouTube API v3 error information:

https://developers.google.com/youtube/v3/docs/errors

"This error is commonly seen if you try to use the OAuth 2.0 Service Account flow. YouTube does not support Service Accounts, and if you attempt to authenticate using a Service Account, you will get this error."

Is this correct? I cannot use a service account to upload video to YouTube automatically?

(that was a waste of a couple of days hard work!)

Tommie answered 13/12, 2014 at 10:30 Comment(2)
try thisIa
Thanks Claud for this. I was about to go this path just now. You alerted me.Brutish
T
12

Yes, it is correct.

The way forward is to do a manual authorisation and grab the resultant 'refresh token' and then use that for any automated uploads.

Ensure to add the refresh token to the PHP Google_Client object prior to any other action.

I am now automatically uploading to YouTube.

Tommie answered 14/12, 2014 at 20:38 Comment(3)
Thanks! I had exactly the same problem and your own answer gave me the helpful hint. For those who have also this problem: something called "offline access" is needed. google for refresh_token and offline access. You need a "Client ID for web application" to get a refresh_token. For all the other requests you need the client ID and the refresh_token. Here is a helpful link developers.google.com/accounts/docs/OAuth2WebServer#offlineSpringtail
and of course you also need a client secret all the time ;)Springtail
domsammut.com/code/… is a good reference for how to do thisPotentate
T
1

For anyone attempting to do this today, be aware that any uploads will be set as "Private (Locked)" if they are uploaded using the API unless/until the app is submitted for verification and approved.

https://github.com/porjo/youtubeuploader/issues/86

YouTube requires verification of the OAuth App/credentials used for upload, otherwise the video will be locked as Private.

It's possible to get an app approved, but if you're doing a personal project, it's less likely.

More: https://github.com/porjo/youtubeuploader/issues/86

Toughminded answered 30/3, 2022 at 22:48 Comment(0)
J
1

Yes you can use Service Accounts for Youtube. One way is to add the service account to a CMS where the content owner is part of. When you retrieve an access_token for the Service Account, you can work with the content owners videos.

Looks like this, using Javascript and a npm package:

import { google } from 'googleapis';
const youtube = google.youtube('v3');

const fetchYoutubeData = async (keyfile, scopes) => {
  const auth = new google.auth.GoogleAuth({
      credentials: keyfile,
      scopes,
  });

  const accessToken = await auth.getAccessToken();

  const { data } = await youtube.videos.list({
      part: ['snippet'],
      id: 'YOUR_VIDEO_ID',
      access_token: accessToken
  });

  return data;
}

Note that the keyfile is the JSON you downloaded from Gcloud when creating the service accounts keys. NOT the path pointing to the file! If you want to use path, use this instead:

const auth = new google.auth.GoogleAuth({
    keyFile: 'PATH_TO_KEYFILE_IN_FILE_SYSTEM',
    scopes: ['READ_STUFF', 'WRITE_STUFF'],
});

Further reading on server-server authentication using Oauth2

Jacky answered 1/4, 2022 at 13:13 Comment(2)
Your solution suggests that all operations are allowed with a service account which is not true from my understanding. While you can perform searches (e.i list), you cannot perform video uploads.Elevator
Do you have a reference, where it's specified that the insert-operation does not work? From what I know it works, when using the right scopes and having the right permissions granted. You may need to setup domain-wide delegation for your service account. But would be nice to know, if I'm wrong. Then the answer is in the wrong place, I guess...Jacky
I
0

The problem is that Youtube does not immediately approve service accounts, when we set them in Youtube Studio permissions

In the case of using Content Owner on Youtube, acceptance of the service account is immediate.

Adding Youtube Permission

Indelicate answered 26/8, 2023 at 22:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.