从Google Apps脚本访问Twitter API [英] accessing Twitter API from Google Apps Script
本文介绍了从Google Apps脚本访问Twitter API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Google工作表中阅读我的Twitter时间轴.我复制了 GAS文档中报告的有关Twitter身份验证的以下代码(省略了步骤2,因为我没有在UI中使用代码):
I'm trying to read in a Google sheet my Twitter timeline. I've copied the following code reported in the GAS documentation about twitter authentication (omitting step 2 since I'm not using the code inside a UI):
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.
return OAuth1.createService('twitter')
// Set the endpoint URLs.
.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
// Set the consumer key and secret.
.setConsumerKey('mykey')
.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() {
var twitterService = getTwitterService();
var response = twitterService.fetch('https://api.twitter.com/1.1/statuses/user_timeline.json');
Logger.log(response);
}
但是我收到消息错误:服务未授权.(第292行,文件服务",项目"OAuth1").
but I obtain the message error: Service not authorized. (row 292, file "Service", project "OAuth1").
怎么了?
推荐答案
我第一次执行 makeRequest
时需要添加以下行:
I needed to add the following line the first time I execute makeRequest
:
var authorizationUrl = twitterService.authorize();
Logger.log(authorizationUrl);
然后,打开从日志中读取的url并授权该应用.
Then, open the url read from the log and authorize the app.
之后,一切正常.
这篇关于从Google Apps脚本访问Twitter API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文