从 Google Apps 脚本访问 Twitter API

accessing Twitter API from Google Apps Script

我正在尝试阅读 Google sheet 我的 Twitter 时间线。 我复制了 GAS documentation 中关于 Twitter 身份验证的以下代码(省略了第 2 步,因为我没有使用 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 行,文件 "Service",项目 "OAuth1")。

怎么了?

我需要在第一次执行时添加以下行 makeRequest:

var authorizationUrl = twitterService.authorize();
Logger.log(authorizationUrl);

然后,打开url读取日志并授权应用。

之后一切正常。