本文介绍了Google应用脚本oauth连接不适用于trello的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在中使用oAuth来访问trello数据,但它似乎OAuthService API对oAuth服务做了一些假设,并且trello无法如此工作。



以下代码有效。它可以访问twitter(这是来自谷歌的oauth教程):

pre $ function $ authorizeToTwitter(){
var oauthConfig = UrlFetchApp.addOAuthService( 推);
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);
}

下面的代码会让我进入trello按下确认键返回页面,但trello不知道如何重定向回去,所以我进入一个页面,要求我手动复制粘贴一个令牌(但谷歌没有提供给我一个插入该令牌的方法)。

  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);
}

我试图通过手动添加我提供给twitter的重定向回调在授权url中

  oauthConfig.setAuthorizationUrl(https://trello.com/1/OAuthAuthorizeToken?return_url=https: //docs.google.com/macros); //这是教程中说我应该提供给twitter 

  oauthConfig.setAuthorizationUrl(https://trello.com/1/OAuthAuthorizeToken?return_url=https://docs.google.com/macros/externaloauthcallback ); //这是Twitter在执行oauth舞蹈时实际调用的内容

但两者都不起作用。难道我做错了什么?我是否缺少一些我应该提供的配置参数?

解决方案

这是由于; Google在获得授权令牌时尝试提供 oauth_callback ,但当您批准令牌请求时,Trello未在那里重定向。



这个错误已经解决了,我已经验证了下面的代码有效:

$ p $ 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);

//将这些值替换为您从
获得的值// https://trello.com/1/appKey/generate
oauthConfig.setConsumerKey(Consumer Key) ;
oauthConfig.setConsumerSecret(Consumer Secret);
$ b $ 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());
}


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?

解决方案

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());
}

这篇关于Google应用脚本oauth连接不适用于trello的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 18:36