0

I've been trying to authenticate to a 3rd party service using oAuth and I get an "Unexpected error" at the UrlFetchApp.fetch() stage. This prompted me to replace the 3rd party service with Twitter whose example is provided in one of the tutorials and I see the same problem with twitter too.

Here is my code.

function testTwitter() {
  var oauth = UrlFetchApp.addOAuthService('twitter');
  oauth.setRequestTokenUrl('https://api.twitter.com/oauth/request_token');
  oauth.setAuthorizationUrl('https://api.twitter.com/oauth/authorize');
  oauth.setAccessTokenUrl('https://api.twitter.com/oauth/access_token');
  oauth.setConsumerKey('CONSUMER_KEY');
  oauth.setConsumerSecret('CONSUMER_SECRET');

  var url = 'https://api.twitter.com/1/statuses/mentions.json'; 
  try{
    var options = {
      "oAuthServiceName" : "twitter",
      "oAuthUseToken" : "always",
      "method":"GET"
    };
     var resp = UrlFetchApp.fetch(url,options);
      Logger.log(resp.getContentText());
  }catch(ex){
    Logger.log(ex);
  }
}

When I see the logs, I see the following being thrown at the UrlFetchApp.fetch stage.

Exception: Unexpected error: 

Am I missing something ? Is the Callback URL necessary ? If yes, where do I specify it ?

1
  • I had the same problem. Using RestClient browser addon helped. In the end, it turned out to be the options = { .... they needed to match what the server was expecting. Commented Oct 18, 2012 at 9:01

2 Answers 2

2

I've used your code with twitter and it works fine if you follow the tutorial from the Apps Script page: https://developers.google.com/apps-script/articles/twitter_tutorial

Mainly,check the Setting Up Twitter section. You must setup your twitter to work with your script. Here is the link to set up new app for your twitter: http://dev.twitter.com/apps/new

EDIT: This should also apply to the other 3rd party service you are using.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Anton. After adding a callback URL to my twitter app, I was able to work with twitter. But still no luck with the 3rd party API. I've written to their support staff and hopefully something should come out
1

I created a version of the script above, set up Twitter as per the tutorial above (including the specified callback URL) and everything was working great...until yesterday afternoon.

     function tweet() {
   // Setup OAuthServiceConfig
   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(ScriptProperties.getProperty("twitterConsumerKey"));
   oAuthConfig.setConsumerSecret(ScriptProperties.getProperty("twitterConsumerSecret"));

   // Setup optional parameters to point request at OAuthConfigService.The "twitter"
   // value matches the argument to "addOAuthService" above.
   var options =
     {
        muteHttpExceptions : true,
       "oAuthServiceName" : "twitter",
       "oAuthUseToken" : "always"
     };
   var_url='https://api.twitter.com/1.1/search/tweets.jsonq=hello&count=5&include_entities=true&result_type=recent';
   var result = UrlFetchApp.fetch(url,options);
   var tweets  = Utilities.jsonParse(result);

For some reason, I'm now getting an OAuth error. I added muteHTTPExceptions = true and the message says "Failed to authenticate for service: twitter".

I've done the following:

  • checked the execution file and can see my key and secret being pulled through from the project properties

  • generated a CURL from Twitter with the same key and secret to double check that these are working (they are and I get the anticipated response)

  • Deployed my google script as a webapp (just in case there was some sort of permissions error here)

Does anybody have ideas for what else I can check? Kinda stuck here...

Any help much appreciated :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.