使用 Apps 脚本访问 tumblr API 时遇到问题

Getting trouble accessing tumblr API using Apps Script

我正在尝试使用 Google Apps 脚本 post 到 tumblr。我了解到 Tumblr 使用 OAuth V1。为了得到一个想法并测试 API 我复制了 GSuiteDevs Apps Script OAuth1 Twitter Sample code available at Github.

我已经根据需要做了适当的修改。在 运行 脚本之后,我得到一个错误 400.8001 根据 Tumblr API Documentation 是由于

"when an NPF JSON parameter is invalid or a bad format".

代码和错误如下:

var CONSUMER_KEY = 'XXXVQoZ0kLUYB7GDHzJZcXXXXXXXXXXXXXXXXXXXXXXXX';
var CONSUMER_SECRET = 'XXXXlLVQS2z3WpXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

/**
 * Authorizes and makes a request to the Tumblr API.
 */
function run() {
  var service = getService();
  if (service.hasAccess()) {
    var url = 'https://api.tumblr.com/v2/blog/{MY BLOG IDENTIFIER COMES HERE}.tumblr.com/posts';
    var payload = {
      "content": [
        {
            "type": "text",
            "text": "Hello world!"
        }
    ]
    };
    var response = service.fetch(url, {
      method: 'post',
      payload: payload
    });
    var result = JSON.parse(response.getContentText());
    Logger.log(JSON.stringify(result, null, 2));
  } else {
    var authorizationUrl = service.authorize();
    Logger.log('Open the following URL and re-run the script: %s',
        authorizationUrl);
  }
} 

/**
 * Reset the authorization state, so that it can be re-tested.
 */
function reset() {
  var service = getService();
  service.reset();
}

/**
 * Configures the service.
 */
function getService() {
  return OAuth1.createService('Tumblr')
      // Set the endpoint URLs.
      .setAccessTokenUrl('https://www.tumblr.com/oauth/access_token')
      .setRequestTokenUrl('https://www.tumblr.com/oauth/request_token')
      .setAuthorizationUrl('https://www.tumblr.com/oauth/authorize')

      // Set the consumer key and secret.
      .setConsumerKey(CONSUMER_KEY)
      .setConsumerSecret(CONSUMER_SECRET)

      // Set the name of the callback function in the script referenced
      // above that should be invoked to complete the OAuth flow.
      .setCallbackFunction('authCallback')

      // Using a cache will reduce the need to read from 
      // the property store and may increase performance.
      .setCache(CacheService.getUserCache())

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties());
}

/**
 * Handles the OAuth callback.
 */
function authCallback(request) {
  var service = getService();
  var authorized = service.handleCallback(request);
  if (authorized) {
    return HtmlService.createHtmlOutput('Success!');
  } else {
    return HtmlService.createHtmlOutput('Denied');
  }
}

错误如下图:

Exception: Request failed for https://api.tumblr.com returned code 400. Truncated server response: {"meta":{"status":400,"msg":"Bad Request"},"response":[],"errors":[{"title":"Bad Request","code":8001,"detail":"Posting failed. Please try again."}]} (use muteHttpExceptions option to examine full response) (line 457, file "Service")

代码中有什么问题?

感谢@TheMaster

错误在于不包括 contentType:"application/json"JSON.stringify(payload) 。这些都需要包括在内。