I'm looking to write a program in Google apps script that keeps an instance of my CSE up to date by regularly posting an "annotations" XML file containing certain urls. I have a properly formatted XML file to be posted to the CSE, however I'm a little unclear about how to authorize the post using Apps Script's UrlFetchApp. Here's what I have so far:
var userId = "my user ID";
var cseID = "my custom search engine ID";
var xml = an xml blob;
var auth = ScriptApp.getOAuthtoken();
var cseUrl = "http://cse.google.com/api/"+userId+"/annotations/"+cseID;
var params = {
"method" : "POST",
"contentType" : "application/xml; charset=utf-8",
"payload" : xml,
"muteHttpExceptions" : true,
"Authorization" : "GoogleLogin auth=" + auth
};
var response = UrlFetchApp.fetch(cseUrl, params)
However I'm getting <Error>You are not authorized to access this resource. If you feel this is an error, try re-logging into your Google Account.</Error>
<Error>You are not authorized to access this resource. If you feel this is an error, try re-logging into your Google Account.</Error>
as the response. Should the "auth" variable in this case be the auth token I get from ScriptApp.getOAuthToken()
, or should that be the API key from the Google developer Dashboard?
I've been following their tutorial here , but I'm fairly new to using REST, so any pointers are much appreciated.
You need to add your Authorization to the headers parameter in the params object. https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#getRequest(String,Object)
Check the edit below.
var params = {
"method" : "POST",
"contentType" : "application/xml; charset=utf-8",
"payload" : xml,
"muteHttpExceptions" : true,
"headers":{"Authorization" : "GoogleLogin auth=" + auth}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.