gpt4 book ai didi

google-apps-script - 使用 Apps 脚本访问 tumblr API 时遇到问题

转载 作者:行者123 更新时间:2023-12-04 10:11:29 25 4
gpt4 key购买 nike

我正在尝试使用 Google Apps 脚本发布到 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) .那些需要包括在内。

关于google-apps-script - 使用 Apps 脚本访问 tumblr API 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61319472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com