14

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

Claud
  • 937
  • 1
  • 12
  • 27
  • [try this](http://stackoverflow.com/questions/28114248/upload-video-to-youtube-without-signing-in-in-the-browser/28840178#28840178) – mindw0rk Mar 03 '15 at 19:26
  • Thanks Claud for this. I was about to go this path just now. You alerted me. – floCoder Jun 14 '17 at 00:13

4 Answers4

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.

Claud
  • 937
  • 1
  • 12
  • 27
  • 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 https://developers.google.com/accounts/docs/OAuth2WebServer#offline – user1030151 Feb 13 '15 at 10:32
  • and of course you also need a client secret all the time ;) – user1030151 Feb 13 '15 at 10:40
  • 2
    https://www.domsammut.com/code/php-server-side-youtube-v3-oauth-api-video-upload-guide is a good reference for how to do this – Brett Pennings Jun 14 '15 at 21:16
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

Tronathan
  • 6,473
  • 5
  • 22
  • 24
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

AndyW
  • 482
  • 7
  • 19
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