简体   繁体   English

如何通过Google文档脚本授权和发布/更新Trello卡

[英]How to authorize and post/update Trello card from a Google docs script

I have a Google Docs Spreadsheet that I'd like to use to update referenced cards in Trello. 我有一个Google文档电子表格,我想用来更新Trello中的参考卡片。 I've had some success with oauth and pulling data via their HTTP API, but am stuck with the following: 我在使用oauth并通过其HTTP API提取数据方面取得了一些成功,但是遇到了以下问题:

1) it seems Trello's code.js requires a window object, which the Google Doc script doesn't provide. 1)似乎Trello的code.js需要一个window对象,而Google Doc脚本没有提供。 So, I am stuck using their HTTP API. 因此,我坚持使用他们的HTTP API。

2) authenticating via OAuth works, but only gives me read access. 2)通过OAuth进行身份验证有效,但仅授予我读取访问权限。 I cannot update cards with the token I am able to get. 我无法使用能够获得的令牌更新卡。

function test() {
  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/authorize?key=" + consumerKey + "&name=trello&expiration=never&response_type=token&scope=read,write");
  //oauthConfig.setAuthorizationUrl("https://trello.com/1/OAuthAuthorizeToken");  <-- this only gives read access.  Cannot POST
  oauthConfig.setConsumerKey(consumerKey);
  oauthConfig.setConsumerSecret(consumerSecret);

  var url = 'https://trello.com/1/cards/yOqEgvzb/actions/comments&text=Testing...';  
  var postOptions = {"method" : "post",
                   "oAuthServiceName": "trello",
                   "oAuthUseToken": "always"};

   var response = UrlFetchApp.fetch(url, postOptions);  // "Request failed for returned code 404. Truncated server response: Cannot POST"

   Logger.log(response.getContentText());
}

I've found a number of related questions but no direct answers: 我发现了许多相关问题,但没有直接答案:

How to get a permanent user token for writes using the Trello API? 如何使用Trello API获得用于写入的永久用户令牌?

Trello API: vote on a card Trello API:对卡片进行投票

Trello API: How to POST a Card from Google Apps Script (GAS) Trello API:如何从Google Apps脚本(GAS)发布卡

Google apps script oauth connect doesn't work with trello Google Apps脚本OAuth Connect不适用于trello

Many thanks ahead of time for any advice. 非常感谢您的任何建议。

In order to get write access, you need to change the authorization url. 为了获得写访问权,您需要更改授权URL。 This example works for me 这个例子对我有用

  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?scope=read,write");

on 1) yes you cant use the library from server gas, its meant to be run from a browser. 关于1)是的,您不能使用服务器气体提供的库,它只能从浏览器运行。 on 2), Ive done it from GAS with write access without problems. 在2)上,我已经通过GAS做到了这一点,并且没有问题。 You need to use the format: https://api.trello.com/1/.../xxxx?key=yyyyyy&token=zzzzzzz& ... 您需要使用以下格式: https//api.trello.com/1/.../xxxxkey = yyyyyy&token = zzzzzzz& ...

and when you get the token, you need to request permanent access (no expiration) and write access, as in: https://trello.com/1/authorize?key= "+key+"&name=xxxxxxx&expiration=never&response_type=token&scope=read,write" 并且当您获得令牌时,您需要请求永久访问(没有到期)并进行写访问,例如: https : //trello.com/1/authorize? key = “ + key +”&name = xxxxxxx&expiration = never&response_type = token&scope =读,写”

As in: 如:

function postNewCardCommentWorker(cardId, comment, key, token) {

  var commentEncoded=encodeURIComponent(comment);
  var url = "https://api.trello.com/1/cards/"+cardId+"/actions/comments?text="+commentEncoded+"&key="+key+"&token="+token;
  var options =
     {
       "method" : "POST"
     };

  UrlFetchApp.fetch(url, options);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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