簡體   English   中英

在Apps腳本中實施Google自定義搜索API

[英]Implementing Google Custom Search API in Apps Script

我希望用Google Apps腳本編寫一個程序,通過定期發布包含某些網址的“注釋” XML文件來使我的CSE實例保持最新。 我有一個格式正確的XML文件要發布到CSE,但是我不清楚如何使用Apps Script的UrlFetchApp對帖子進行授權。 這是我到目前為止的內容:

  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)

但是我得到<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>作為響應。 在這種情況下,“ auth”變量應該是我從ScriptApp.getOAuthToken()獲得的auth令牌,還是從Google開發人員儀表板獲得的API密鑰?

我一直在這里關注他們的教程,但是我對使用REST還是比較陌生,因此非常感謝任何指針。

您需要將授權添加到params對象中的headers參數中。 https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#getRequest(String,Object)

檢查下面的編輯。

var params = {
    "method"  : "POST",
    "contentType" : "application/xml; charset=utf-8",
    "payload" : xml,
    "muteHttpExceptions" : true,
    "headers":{"Authorization" : "GoogleLogin auth=" + auth}
  };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM