0

I want to upload a video to YouTube using Google Apps Script. Here is my code, including what I've tried and what the errors are.

Code.gs
/**
 * @see https://developers.google.com/youtube/v3/docs/videos/insert
 * @param { String } sourceUrl url location of the source video file in google drive
 * @returns { Video }
 */
const uploadVideo2youtube = sourceId => {
  const sourceFile = DriveApp.getFileById( sourceId, ).getBlob();
  const videoResource = {
    snippet: {
      title: 'Summer vacation in California',
      description: 'Had a great time surfing in Santa Cruz',
      tags: [ 'surfing', 'Santa Cruz', ],
      categoryId: '22',
    },
  };
  // const status = { privacyStatus: 'Private', };
  const status = { privacyStatus: 'private', };

  // here are all my attempts and the errors with each one...
  // const newVideo = YouTube.Videos.insert( videoResource, status, sourceFile, ); // GoogleJsonResponseException: API call to youtube.videos.insert failed with error: '{privacyStatus=private}' (line 229, file "Code")
  // const newVideo = YouTube.Videos.insert( videoResource, sourceFile, ); // GoogleJsonResponseException: API call to youtube.videos.insert failed with error: Blob (line 230, file "Code")
  const newVideo = YouTube.Videos.insert( videoResource, [ status ], sourceFile, ); // GoogleJsonResponseException: API call to youtube.videos.insert failed with error: '{privacyStatus=private}' (line 231, file "Code")

  Logger.log('(line 213) newVideo:\n%s', JSON.stringify( newVideo ),);
}
const upload2youtube_test = () => upload2youtube( <fileId>, )

As you can see from the code, I have made several attempts at the YouTube.Videos.insert() method. But each attempt throws an error. Yes, I have enabled the YouTube Data API v3 resource under my resources tab and authorized the script to run.

What am I doing wrong?

1 Answer 1

3

How about this modification?

Modification points:

  • Please include status in videoResource.
  • The 2nd argument of YouTube.Videos.insert(resource, part, mediaData) is part. In this case, it's "snippet,status".

Modified script:

const sourceFile = DriveApp.getFileById(sourceId).getBlob();
const videoResource = {
  snippet: {
    title: 'Summer vacation in California',
    description: 'Had a great time surfing in Santa Cruz',
    tags: [ 'surfing', 'Santa Cruz', ],
    categoryId: '22',
  },
  status: {privacyStatus: 'private'}  // Added
};
const newVideo = YouTube.Videos.insert( videoResource, "snippet,status", sourceFile);  // Modified

Note:

  • In this case, it is required to enable YouTube Data API v3 at Advanced Google services. Please be careful this. Ref

References:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.