我对 oauth 和 API 完全陌生,我正在尝试使用一些 示例应用程序脚本 oauth 代码来访问 Basecamp API 以在 Tumblr 上创建帖子:
var npfPost = {
"content": [
{
"type": "text",
"text": "Hello world!"
}
]
}
function run() {
var service = getService_();
if (service.hasAccess()) {
var url = 'https://api.tumblr.com/v2/blog/{blog_id}.tumblr.com/posts';
var options = {
'method' : 'post',
'muteHttpExceptions' : true,
npfPost,
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
}
var response = UrlFetchApp.fetch(url, options);
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
} else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
'https://' + authorizationUrl);
}
}
function reset() {
getService_().reset();
}
function getService_() {
return OAuth2.createService('Tumblr')
// Set the endpoint URLs.
.setAuthorizationBaseUrl('www.tumblr.com/oauth2/authorize')
.setTokenUrl('api.tumblr.com/v2/oauth2/token')
// Set the client ID and secret.
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
// Set the name of the callback function that should be invoked to
// complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
}
function authCallback(request) {
var service = getService_();
var authorized = service.handleCallback(request);
if (authorized) {
return HtmlService.createHtmlOutput('Success!');
} else {
return HtmlService.createHtmlOutput('Denied.');
}
}
function logRedirectUri() {
Logger.log(OAuth2.getRedirectUri());
}
我已经成功生成了一个授权 URL,我希望在授权访问后将其直接发送到 authCallback 函数。相反,我从应用程序脚本中收到以下错误:
Error: Error retrieving token: 403: {"meta":{"status":403,"msg":"Forbidden"},"response":[],"errors":[{"title":"Forbidden","code":5006,"detail":"A secure connection (https) is required to access the Tumblr API"}]} (line 605, file "Service")
我需要保护连接的哪一部分?
从OP的回复
Good catch, that did the trick! Thank you so much
中,我明白我的猜测是解决OP问题的正确方法。在这种情况下,我认为该解决方案可能对其他用户有用。所以,我将其发布为以下答案。
在此修改中,通过添加
https://
解决了该问题,如下所示。
.setAuthorizationBaseUrl('www.tumblr.com/oauth2/authorize')
.setTokenUrl('api.tumblr.com/v2/oauth2/token')
.setAuthorizationBaseUrl('https://www.tumblr.com/oauth2/authorize')
.setTokenUrl('https://api.tumblr.com/v2/oauth2/token')