Cannot upload from Drive via YouTube Data API in G

2019-02-19 05:14发布

I am trying to import a list of files from Google Drive to YouTube. The meta-data and the URL to the file are in a Google spreadsheet, so I wrote some code using Google Apps Script that does the following

  1. Get the selected rows
  2. Retrieve title, description, Google Drive URL
  3. Load the file from Google Drive via DriveApp.getFileById
  4. Upload the blob to YouTube using the title and description via YouTube.Videos.insert
  5. Update the selected row with the YouTube video id from the response

The upload looks something like this

var blob = DriveApp.getFileById(id).getBlob();
var resource = {
    snippet: {
        title: 'The title',
        description: 'A long description ...',
        defaultLanguage: 'de',
        categoryId: 17,
        tags: [ 'Sport', 'Fitness' ],
    },
    status: {
        privacyStatus: 'unlisted'
    }
}
try {
    var result = YouTube.Videos.insert(resource, "snippet,status", blob);
    return result.id;
} catch (err) {
    console.log({message: 'Error ' + err.message, error: err});
}

This code has already worked about a year ago. I have adapted it slightly, but now I do not get a response from the YouTube.Videos.insert call. The following is logged inside the catch:

message: Error Empty response

error: Exception: Empty response

Not very helpful.

Before uploading, I do a YouTube.Channels.list to get a target channel in case there are multiple channels available. For this request, I have to permit access to my data and I am only asked on the first invocation. I also see the script in the list of applications for my Google account. I assume permissions are ok.

Any suggestions on how I can get more information on the issue, or is there something I should do differently?


Regarding the target channel (and this might be a different question), I cannot really use this, as it seems I can only upload to a specific channel, if I am a YouTube content partner (see parameters onBehalfOfContentOwner and onBehalfOfContentOwnerChannel):

Note: This parameter is intended exclusively for YouTube content partners.

1条回答
冷血范
2楼-- · 2019-02-19 05:32

I had same problem in my project and here's what I have figured out: if your video file size is more than 10 Mb, you will get Empty response error.

Probably (can't say officialy because no documentation mentions it) this is happening because Google Apps Script's YouTube.Videos.insert (and all other available built-in services) uses UrlFetchApp under the hood, which have restriction of 10 Mb per call: https://developers.google.com/apps-script/guides/services/quotas#current_limitations. You can check it yourself using your sample code: if file is under 10 Mb, it will be uploaded successfully.

As possible workaround, you can use idea from this answer: https://stackoverflow.com/a/44853845/555121

Basically, you will need to open modal window using SpreadsheetApp.getUi().showModalDialog and then perform upload to YouTube via plain JavaScript inside modal dialog, which have no restrictions on transferred data size. Here's good example of YouTube resumable upload implementation: https://github.com/sangnvus/2015SUMJS01/blob/master/WIP/Sources/FlyAwayPlus/FlyAwayPlus/Scripts/youtube-upload.js

查看更多
登录 后发表回答