【问题标题】:Using Google Apps Script and the Pastebin.com API to post a paste使用 Google Apps 脚本和 Pastebin.com API 发布粘贴
【发布时间】:2018-10-22 16:48:15
【问题描述】:

我正在尝试使用电子表格脚本编辑器中的 Google Apps 脚本 制作 Pastebin.com 粘贴。谁能告诉我我做错了什么?

function postPastebinPost() {
  var options, url, apiKey, payload, response;

  apiKey = <api key goes here>;
  payload = 'Hello World';

  options = {
    'method' : 'post',
    'payload' : payload
  };

  url = 'https://pastebin.com/api/api_post.php'
    + '?api_dev_key=' + apiKey
    + '&api_option=paste'
    + '&api_paste_code=' + encodeURIComponent(payload);

  response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}

我运行它,我的日志显示为Bad API request, invalid api_option。我已经搜索了解决方案,但我没有找到任何解决方案。

文档:

Pastebin.com API

• Google Apps 脚本的UrlFetchApp Class

【问题讨论】:

    标签: google-apps-script google-sheets pastebin google-apps-script-api


    【解决方案1】:

    参数应该在 POST 请求的负载中传递。

    function postPastebinPost() {
    
      var apiKey = 'YOUR KEY GOES HERE';
      var text = 'Hello World';
    
      var payload = {
        api_dev_key: apiKey,
        api_option: 'paste',
        api_paste_code:  text
      };
    
      var options = {
        method : 'POST',
        payload: payload
      };
    
      var url = 'https://pastebin.com/api/api_post.php';
    
      var response = UrlFetchApp.fetch(url, options);
      Logger.log(response.getContentText());
    }
    

    【讨论】:

    • 以防万一,我认为值得添加 {payload: JSON.stringify(payload)}。
    【解决方案2】:

    如果用户想要创建一个新的粘贴作为他们自己的 Pastebin 帐户的一部分(而不是 «Paste as a guest»),以下情况是。这只是对 Amit Agarwal 的回答的改编。

    function postPastebinPost() {
      var title = 'abc';
      var contents = 'Hello World \n next line of content  \n more text';
      var payload = {
        api_dev_key: 'aa6f3ab...', // https://pastebin.com/api#1
        api_option: 'paste',
        api_paste_name: title,
        api_paste_code: contents,
        api_paste_private: '0', // public paste
        api_user_name: 'diccionario...', // name of your Pastebin account
        api_user_password: 'dk398d...', // password to your Pastebin account
        api_user_key: '39dk3...', // https://pastebin.com/api/api_user_key.html
        };
      var options = {
        method : 'POST',
        payload: payload
        };
      var url = 'https://pastebin.com/api/api_post.php';
      var response = UrlFetchApp.fetch(url, options);
      Logger.log(response.getContentText());
    }
    

    整个文档位于https://pastebin.com/api

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 2014-03-05
      相关资源
      最近更新 更多