Question

I am trying to call Twitter's REST API from Google Apps Script. The code I am using is copied one to one from here: https://developers.google.com/apps-script/guides/services/external

and specifically:

function test() {
  var consumerKey = 'XXXX';     // Copied from my twitter app setting.
  var consumerSecret = 'XXXXX'; // Copied from my twitter app setting.
  var oauthConfig = UrlFetchApp.addOAuthService('twitter');
  oauthConfig.setAccessTokenUrl('http://api.twitter.com/oauth/access_token');
  oauthConfig.setRequestTokenUrl('http://api.twitter.com/oauth/request_token');
  oauthConfig.setAuthorizationUrl('http://api.twitter.com/oauth/authorize');
  oauthConfig.setConsumerKey(consumerKey);
  oauthConfig.setConsumerSecret(consumerSecret);
  var options = {
      'oAuthServiceName' : 'twitter',
      'oAuthUseToken' : 'always'
  };

  var url = 'https://api.twitter.com/1.1/search/tweets.json?count=5&include_entities=false&result_type=recent&q=hello';
  var response = UrlFetchApp.fetch(url, options);
  var tweets = JSON.parse(response.getContentText());
  Logger.log(tweets)  
}

Nevertheless, I get Oauth ERROR every time I run it. My settings in the twitter app allows for "Read and Write"

Any suggestions

Was it helpful?

Solution

Just go to your twitter application and set the callback url as https://script.google.com

set the callback url as the image

OTHER TIPS

This is now best handled through the following open source Google Apps Script library:

https://github.com/googlesamples/apps-script-oauth1

The example in the documentation is twitter itself (because it's probably the only remaining user of oauth v1 out there...)

Cheers, M

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top