簡體   English   中英

PUT 請求 - 谷歌腳本 url 獲取問題

[英]PUT request - google script url fetch issue

我在使用 put 函數時遇到問題。 任何人都可以找出錯誤? 謝謝

卷曲——

curl --request PUT \
     --url https://open-api.guesty.com/v1/listings/XXXXXX/custom-fields \
     --header 'accept: application/json' \
     --header 'authorization: BEARERXXX' \
     --header 'content-type: application/json' \
     --data '
{
     "customFields": [
          {
               "fieldId": "5f744b491af840002ca636a2",
               "value": "Door code"
          }
     ]
}
'

我的谷歌腳本:

function putlistingcustomfield (){
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('get_token');// You can change the sheets name here to match your needs
  var code = sheet.getRange("A18").getValue();

  const myHeaders2 = {
    Accept: 'application/json',
    Authorization: code,
  };
var data = {
    'customFields': {
      'fieldId': '639cdb92e128c3002a11b186',
      'value': 'test'
    },
  
};

    const options = {
    method: 'PUT', 
    headers: myHeaders2,
    followRedirects: true,
    payload: JSON.stringify(data),
  };

  const url = 'https://open-api.guesty.com/v1/listings/XXXXXX/custom-fields';
  let response2 = UrlFetchApp.fetch(url, options);
  Logger.log(response2);

    var sheet2 = ss.getSheetByName('write');// You can change the sheets name here to match your needs
    var cell2 = sheet2.getRange("A10");
      cell2.setValue(response2);

  };

我正在嘗試使用來自 google 腳本的 PUT 請求推送數據,GET 正在工作,但無法弄清楚 PUT,如何使用 google urlfetch 實現它

從您顯示的示例 curl 命令中,進行以下修改如何?

修改腳本:

function putlistingcustomfield() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('get_token');
  var code = sheet.getRange("A18").getValue();
  const myHeaders2 = {
    Accept: 'application/json',
    Authorization: code, // Here, the value of `code` is required to be `Bearer XXX`. Please be careful about this.
  };
  var data = { 'customFields': [{ 'fieldId': '639cdb92e128c3002a11b186', 'value': 'test' }] }; // Modified
  const options = {
    method: 'PUT',
    headers: myHeaders2,
    // followRedirects: true,
    payload: JSON.stringify(data),
    contentType: 'application/json', // Modified
  };
  const url = 'https://open-api.guesty.com/v1/listings/XXXXXX/custom-fields';
  let response2 = UrlFetchApp.fetch(url, options);
  Logger.log(response2);
  var sheet2 = ss.getSheetByName('write');
  var cell2 = sheet2.getRange("A10");
  cell2.setValue(response2);
}

筆記:

  • 在此修改中,它假設您的令牌和請求正文的值是有效值。 如果出現與它們相關的錯誤,請再次確認它們。

  • 從您的錯誤消息中,我注意到需要修改請求正文的值。 所以,我反思了一下。

參考:

暫無
暫無

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

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