【问题标题】:accessing Twitter API from Google Apps Script从 Google Apps 脚本访问 Twitter API
【发布时间】:2021-10-04 10:59:26
【问题描述】:

我正在尝试在 Google 表格中读取我的 Twitter 时间线。 我已经复制了GAS documentation 中报告的关于 twitter 身份验证的以下代码(由于我没有在 UI 中使用代码,因此省略了第 2 步):

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”)。

怎么了?

【问题讨论】:

    标签: twitter google-apps-script google-sheets twitter-oauth


    【解决方案1】:

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

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

    然后,打开从日志中读取的url,对应用进行授权。

    之后,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-09
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多