简体   繁体   中英

Making oAuth requests using Google Apps Script

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 ?

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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM