Home > Back-end >  API POST request - Python example to Google Script - 'Unexpected end of JSON input'
API POST request - Python example to Google Script - 'Unexpected end of JSON input'

Time:11-11

I'm trying to connect a Google spreadsheet to snov.io service using an API POST request, they have provided a Python code as example and I have tried to run the google script below based on it, but without success. Any ideas? Thank you :)

Python (provided by the company)

def get_access_token():
params = {
    'grant_type':'client_credentials',
    'client_id':'c57a0459f6t141659ea75cccb393c111',
    'client_secret': '77cbf92b71553e85ce3bfd505214f40b'
}

res = requests.post('https://api.snov.io/v1/oauth/access_token', data=params)
resText = res.text.encode('ascii','ignore')

return json.loads(resText)['access_token']

Google Script (Error:SyntaxError:'Unexpected end of JSON input')

var params = {
    'grant_type':'client_credentials',
    'client_id':'c57a0459f6t141659ea75cccb393c111',
    'client_secret': '77cbf92b71553e85ce3bfd505214f40b',
    'method':'post'
}
var url = 'https://api.snov.io/v1/oauth/access_token';

var response = UrlFetchApp.fetch(url,params)
var json = response.getContentText();
    obj = JSON.parse(json);
    token = obj.result.access_token;
    return token

CodePudding user response:

In order to achieve the same request with your python script, how about modifying your Google Apps Script as follows?

Modified script:

From:

var params = {
    'grant_type':'client_credentials',
    'client_id':'c57a0459f6t141659ea75cccb393c111',
    'client_secret': '77cbf92b71553e85ce3bfd505214f40b',
    'method':'post'
}
var url = 'https://api.snov.io/v1/oauth/access_token';

var response = UrlFetchApp.fetch(url,params)

To:

var params = {
  'grant_type': 'client_credentials',
  'client_id': 'c57a0459f6t141659ea75cccb393c111',
  'client_secret': '77cbf92b71553e85ce3bfd505214f40b',
}
var url = 'https://api.snov.io/v1/oauth/access_token';
var response = UrlFetchApp.fetch(url, {method: "post", payload: params});
  • In this case, even when method: "post" is removed, the same request is achieved.

Reference:

  • Related