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?
Credits to @TheMaster
The errors were in not including contentType:"application/json"
and JSON.stringify(payload)
. Those need to be included.