Google apps script oauth connect doesn't work

2020-06-16 02:42发布

I've been trying to use oAuth in Google apps script to access trello data, but it seems that the OAuthService API makes some assumptions about the oAuth service and that trello doesn't work that way.

The following code works. It get's access to twitter (this is from google's oauth tutorial):

function authorizeToTwitter() {
  var oauthConfig = UrlFetchApp.addOAuthService("twitter");
  oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
  oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
  oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");
  oauthConfig.setConsumerKey(<CONSUMER KEY>);
  oauthConfig.setConsumerSecret(<CONSUMER SECRET>);
  var requestData = {
    "method": "GET",
    "oAuthServiceName": "twitter",
    "oAuthUseToken": "always"
  };
  var result = UrlFetchApp.fetch("https://api.twitter.com/1/statuses/mentions.json", requestData);
}

The following code will get me to the trello "press ok to get back" page, but trello doesn't know how to redirect back, so I get to a page that asks me to manually copy paste a token (but google doesn't provide me with a method of inserting that token)

function authorizeToTrello() {
  var oauthConfig = UrlFetchApp.addOAuthService("trello");
  oauthConfig.setAccessTokenUrl("https://trello.com/1/OAuthGetAccessToken");
  oauthConfig.setRequestTokenUrl("https://trello.com/1/OAuthGetRequestToken");
  oauthConfig.setAuthorizationUrl("https://trello.com/1/OAuthAuthorizeToken");
  oauthConfig.setConsumerKey(<CONSUMER KEY>);
  oauthConfig.setConsumerSecret(<CONSUMER SECRET>);
  var requestData = {
    "method": "GET",
    "oAuthServiceName": "trello",
    "oAuthUseToken": "always"
  };
  var result = UrlFetchApp.fetch(
      "https://api.trello.com/1/members/me/boards",
      requestData);
}

I tried to fix that by manually adding the redirect callback that I provide to twitter in the the authorization url

oauthConfig.setAuthorizationUrl("https://trello.com/1/OAuthAuthorizeToken?return_url=https://docs.google.com/macros"); //this is what the tutorial says I should provide to twitter

or

oauthConfig.setAuthorizationUrl("https://trello.com/1/OAuthAuthorizeToken?return_url=https://docs.google.com/macros/externaloauthcallback"); //this is what twitter actually calls when performing the oauth dance

But both don't work. Am I doing something wrong? Am I missing some configuration parameters that I should be providing?

1条回答
看我几分像从前
2楼-- · 2020-06-16 02:59

This behavior was due to a bug in the Trello API; Google is attempting to provide an oauth_callback when it gets its authorization token, but Trello wasn't redirecting there when you approve the token request.

This bug has since been resolved, and I've verified that the following code works:

function authorizeToTrello() {
  var oauthConfig = UrlFetchApp.addOAuthService("trello");
  oauthConfig.setAccessTokenUrl("https://trello.com/1/OAuthGetAccessToken");
  oauthConfig.setRequestTokenUrl("https://trello.com/1/OAuthGetRequestToken");
  oauthConfig.setAuthorizationUrl("https://trello.com/1/OAuthAuthorizeToken");

  // Replace these with the values you get from 
  // https://trello.com/1/appKey/generate
  oauthConfig.setConsumerKey("Consumer Key");
  oauthConfig.setConsumerSecret("Consumer Secret");

  var requestData = {
    "method": "GET",
    "oAuthServiceName": "trello",
    "oAuthUseToken": "always"
  };

  var result = UrlFetchApp.fetch(
      "https://api.trello.com/1/members/me/boards",
      requestData);

  Logger.log(result.getContentText());
}
查看更多
登录 后发表回答