I’m trying to download ratings data from SensorTower API using Google Apps Script, but I get 401 error.
I can retrieve data using python on my local computer using this simple code
AUTH_TOKEN = 'xxx'
GET_RATINGS_URL = "https://api.sensortower.com:443/v1/ios/review/get_ratings"
payload = {'auth_token': AUTH_TOKEN,
'country':'US',
'app_id': '123456789'
}
product_response = requests.get(GET_RATINGS_URL,params=payload)
but I need to make it work in Google Apps Script. I’ve written this code
AUTH_TOKEN = 'xxx'
GET_RATINGS_URL = "https://api.sensortower.com:443/v1/ios/review/get_ratings"
function callSensortowerAPI(inputURL, authToken) {
const payload = {"auth_token":authToken,
"country":"US",
"app_id":"123456789"
}
var response = UrlFetchApp.fetch(inputURL,payload)
var json = response.getContentText()
var data = JSON.parse(json)
return data
}
var rating_data = callSensortowerAPI(GET_RATINGS_URL,AUTH_TOKEN)
which seems to me similar to what’s working in Python. But I get this error
Exception: Request failed for https://api.sensortower.com returned code 401. Truncated server response: {"error":"You need to sign in or sign up before continuing."}
I tried to stringify the payload like this
var options = {
"method" : "POST",
"contentType": "application/json",
"payload" : JSON.stringify(payload)
var response = UrlFetchApp.fetch(inputURL,options)
}
but it still returned 401.
When I tried to insert random auth_token in Python, it returned {'error': 'Invalid authentication token.'}
so it seems like another issue.
So my main question is why python code is working and Google Apps Script isn’t?
Vit Volsicka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.