Claud
Claud

Reputation: 997

Allowing Google Service Account YouTube Upload Access via API v3

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!)

Upvotes: 16

Views: 8126

Answers (4)

Piero Ribeiro
Piero Ribeiro

Reputation: 1

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

Upvotes: 0

AndyW
AndyW

Reputation: 491

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

Upvotes: 1

Tronathan
Tronathan

Reputation: 6784

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

Upvotes: 1

Claud
Claud

Reputation: 997

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.

Upvotes: 12

Related Questions