【发布时间】:2018-12-29 00:24:48
【问题描述】:
我使用了这里发布的文档https://github.com/gsuitedevs/apps-script-oauth1
我在获取要授权的功能时遇到问题。我是使用 API 的新手,所以请多多包涵。试图从 twitter 发出一个简单的 get 请求,但第一部分没有通过。知道哪里出了问题吗? *注意我加载了 Oauth1 库。
function getTwitterService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
var service = OAuth1.createService('twitter')
// Set the endpoint URLs.
service.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
service.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
service.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
// Set the consumer key and secret.
service.setConsumerKey('myKey')
service.setConsumerSecret('mySecret')
// Set the name of the callback function in the script referenced
// above 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 twitterService = getTwitterService();
var isAuthorized = twitterService.handleCallback(request);
if (isAuthorized) {
return Logger.log('Success! You can close this tab.');
} else {
return Logger.log('Denied. You can close this tab');
}
}
function makeRequest() {
Logger.log(authorizationUrl);
var twitterService = getTwitterService();
var response = twitterService.fetch("https://api.twitter.com/1.1/followers/list.json?screen_name='xyz'");
var post = response.getContentText();
Logger.log(post);
}
}
【问题讨论】:
-
正如@amit 的回答所表明的,如果您打开脚本编辑器,突出显示所有代码,然后按 Tab 执行基于语法的自动缩进,您会注意到您已将方法范围限定为在
getTwitterService()方法中。
标签: json google-apps-script twitter-oauth