2

I am trying to post to tumblr using Google Apps Script. I have learnt Tumblr uses OAuth V1. To get an idea and to test the API I copied the GSuiteDevs Apps Script OAuth1 Twitter Sample code available at Github.

I have suitably modified wherever necessary. After running the script, I am getting an error 400.8001 which according to Tumblr API Documentation is due to

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

The code and the error are provided below:

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');
  }
}

The error is shown below:

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")

What is the problem in the code?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • 1
    try `payload: JSON.stringify(payload)` – TheMaster Apr 20 '20 at 13:11
  • @TheMaster That gives error 401. ``` { "meta": { "status": 401, "msg": "Unauthorized" }, "response": [], "errors": [ { "title": "Unauthorized", "code": 1016, "detail": "Unable to authorize" } ] } ``` –  Apr 20 '20 at 16:12
  • Run `reset()` and try authorizing again. Make sure all your credentials/keys/secrets are right. – TheMaster Apr 20 '20 at 17:18
  • @TheMaster I did it again. Using JSON.stringify gives 401, and without it gives 400. –  Apr 21 '20 at 06:13
  • 400 is bad request. so json should be stringified. 401 is unauthorized. For whatever reason, your authorization credentials are not valid. make sure it isn't a issue with your service provider by testing with curl or postman.if not, try creating a issue in the oauth library github – TheMaster Apr 21 '20 at 06:17
  • 1
    One last thing: try setting `contentType:"application/json"` in `options` with and without `JSON.stringify` – TheMaster Apr 21 '20 at 06:21
  • @TheMaster It worked. :) –  Apr 21 '20 at 06:37
  • `contentType:"application/json"` with `JSON.stringify` Thank you –  Apr 21 '20 at 06:38

1 Answers1

2

Credits to @TheMaster

The errors were in not including contentType:"application/json" and JSON.stringify(payload) . Those need to be included.