Creating a Python Request Equivalent in Google Script

I’m trying to upload a video url to Vimeo in Google Script. I’ve set this up successfully in my Python/Django app with the following:

url = 'https://api.vimeo.com/me/videos'
headers = {'Authorization': 'bearer xxx',
           'Content-Type': 'application/json',
           'Accept': 'application/vnd.vimeo.*+json;version=3.4'}

data = {'upload': {'approach': 'pull',
        'link': 'https://video.mp4'}}

requests.post(url, headers=headers, data=json.dumps(data))

I tried to create the equivalent to the best of my knowledge in Google Scripts as follows:

var url = 'https://api.vimeo.com/me/videos'
var headers = {
'Authorization': 'bearer xxx', 'Content-Type' : 'application/json', 'Accept' : 'application/vnd.vimeo.*+json;version=3.4'}

var data = {'upload' :{ 'approach' : 'pull','link' : 'https://video.mp4'}}
var options = {
  headers:headers,
  data:data,
}

var response = UrlFetchApp.fetch(url,options)

I get a successful response, but it seems that I’m getting a list of existing videos on my account and no video is uploaded. Could anyone provide any insight into how I could reconfigure my Google Script set up. Thank you!

Answer

Your Python code shows a POST request, but the Apps Script UrlFetchApp.fetch() request doesn’t specify the method, so it is defaulting to a GET request. To make it a POST, include method: 'post' in the options.

In addition, the data being posted should be labeled payload in the options:

var url = 'https://api.vimeo.com/me/videos'
var headers = {
'Authorization': 'bearer xxx', 'Content-Type' : 'application/json', 'Accept' : 
'application/vnd.vimeo.*+json;version=3.4'}

var data = {'upload' :{ 'approach' : 'pull','link' : 'https://video.mp4'}}
var options = {
  method: 'post', // Add this line
  headers:headers,
  payload: data, // Change this line.  It may also be necessary to stringify: payload: JSON.stringify(data)
}

var response = UrlFetchApp.fetch(url,options)

Leave a Reply

Your email address will not be published. Required fields are marked *