使用Google Script连接到Twitter API - oAuth错误

时间:2015-08-29 15:23:24

标签: api twitter oauth google-apps-script

我在查找如何通过oAuth身份验证使用Google脚本连接到我的Twitter时遇到了很多麻烦。我是一个新手,我真的不确定我的代码中可能出现的许多错误。

function getTwitterService() {
return OAuth1.createService('twitter')
    .setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
    .setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
    .setAuthorizationUrl('https://api.twitter.com/oauth/authorize')

    .setConsumerKey('xxxxxxxxxxxxxxxxxxxxxxxxx')
    .setConsumerSecret('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

    .setCallbackFunction('authCallback')

    .setPropertyStore(PropertiesService.getUserProperties());
}

function tweet() {
  var options =
   {
     "oAuthServiceName" : "twitter",
     "oAuthUseToken" : "always",
     "method" : "post"
   };

   UrlFetchApp.fetch("https://api.twitter.com/1.1/statuses/update.json?status=Hi", options);
}

我很确定我的Twitter应用程序已获得授权,因为它在我的https://twitter.com/settings/applications中显示为读写应用程序,而且我也解雇了这个函数......

function logAuthorizationUrl() {
  var twitterService = getTwitterService();
  if (!twitterService.hasAccess()) {
    var authorizationUrl = twitterService.authorize();
    Logger.log(authorizationUrl);
  } else {
    Logger.log('We already have access don\'t we?');
  }
}

...它会记录该自定义消息。

我仍然收到错误(抱歉法语复制/粘贴)。

  

Échecdelarequêtepour   https://api.twitter.com/1.1/statuses/update.json?status=Hi。码   renvoyé:400.Réponsetronquéeduserveur:   {“errors”:[{“code”:215,“message”:“Bad Authentication data。”}]}   (Utilisez l'option muteHttpExceptions pour examinerlaréponse   entière。)(ligne 51,fichier“”)

正如您猜测的那样,我的主要问题是{"errors":[{"code":215,"message":"Bad Authentication data."}]}

知道我缺少什么吗?

1 个答案:

答案 0 :(得分:1)

看起来问题是你正在使用POST,但没有指示有效负载 - 请参阅Google Documentation for UrlFetchApp,特别是有效负载部分(它们实际上使用twitter作为示例)。

通常情况下,我会在发布之前测试我的答案,但是这个问题还涉及到更多的问题。关闭推特API文档虽然我有信心至少应该帮助。另外,我确定你已经离开了这里,但以防万一 - 来自twitter的回复将是JSON,this documentation可以帮助你解析。