使用UrlFetchApp.fetch方法的错误请求

时间:2012-06-03 17:43:32

标签: google-apps-script

我正在尝试使用它的api从Pinboard.in获取标签列表但是在执行fetch方法时出现错误。

代码是:

function getTags(user, password) {
  var url ="https://"+user+":"+password+"@api.pinboard.in/v1/tags/get"
  var response=httpGet(url);
  return response;
}
function httpGet(theUrl)
    {
      var options = { "contentType" : "application/xml; charset=utf-8"} ;
      var response = UrlFetchApp.fetch(theUrl,options);
      return response.getContentText();
    }

我做错了什么?

1 个答案:

答案 0 :(得分:4)

不幸的是,UrlFetchApp不支持用户:password @前缀表示法。相反,您必须使用以下代码手动构建Authorization标头:

var response = UrlFetchApp.fetch('https://api.pinboard.in/v1/tags/get', {
  headers: {
    Authorization: 'Basic ' + Utilities.base64Encode(user + ':' + password)
  }
});