2

I have a Google Docs Spreadsheet that I'd like to use to update referenced cards in Trello. I've had some success with oauth and pulling data via their HTTP API, but am stuck with the following:

1) it seems Trello's code.js requires a window object, which the Google Doc script doesn't provide. So, I am stuck using their HTTP API.

2) authenticating via OAuth works, but only gives me read access. 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: vote on a card

Trello API: How to POST a Card from Google Apps Script (GAS)

Google apps script oauth connect doesn't work with trello

Many thanks ahead of time for any advice.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
dixkin
  • 811
  • 2
  • 11
  • 20
  • Is there a reason you're using OAuth over the alternative: https://trello.com/docs/gettingstarted/authorize.html? Client authorization sounds more like what you're looking for. – Aaron Dufour Oct 26 '13 at 16:22
  • That will give me a popup window. Any examples of how to render that and process/store the token for subsequent calls? – dixkin Oct 28 '13 at 22:30

2 Answers2

2

In order to get write access, you need to change the authorization 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");
dparnas
  • 4,090
  • 4
  • 33
  • 52
1

on 1) yes you cant use the library from server gas, its meant to be run from a browser. on 2), Ive done it from GAS with write access without problems. You need to use the format: https://api.trello.com/1/.../xxxx?key=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"

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);
}
Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • regading automating the token acquisition, the most Ive done was to display a link to the user like the one I suggest in the answer, and instruct the user to copy/paste the resulting token. – Zig Mandel Oct 28 '13 at 23:08
  • 1
    Any examples of showing a popup auth page and actually parsing the resulting token? Having the user copy an auth token is a really bad security policy, not to mention a bad user experience. – dixkin Oct 28 '13 at 23:35
  • You wont be able to parse a popup for security cross-domain issues and you can urlfetch it since it rewuires authentication and clicking an approval. However you can give a redirect url to the api which will open your apps script again. In my case I havent put too much effort since the user only installs it once. See https://trello.com/docs/gettingstarted/oauth.html – Zig Mandel Oct 29 '13 at 02:22
  • I've looked at oauth for Trello, but the call only gives me read access. I cannot request more than that through the API. – dixkin Oct 29 '13 at 17:16
  • Yes its possible look at my answer where it uses write on: "https://trello.com/1/authorize?key="+key+"&name=xxxxxxx&expiration=never&response_type=token&scope=read,write" that link will give you a trello key with write access. – Zig Mandel Oct 30 '13 at 01:24
  • Right, I get that this works when asking the user to manually paste the token into some prompt that stores it indefinitely (assuming this is possible in a docs script). I was hoping to find a more seamless solution than this - specifically for Google Docs Spreadsheets. The OAuth Coffee Script example likely won't work in a Google Docs script. – dixkin Oct 30 '13 at 22:36
  • FYI I have updated my post to include an example of what I am testing via a Google Docs Spreadsheet. – dixkin Oct 30 '13 at 23:23
  • 1
    Ok,done some research. Its possible that you might not get it to work using the oauthConfig. Since trello allows for non-expiring tokens, its easy enough to do it yourself manually. To avoid the user copy/paste the token during your setup use the params callback_method" (fragment) and "return_url" to be https://gas_service_url. This will cause that URL along with "token=thetoken"), and you can handle that request and store it globally. You can store it globally on a docs script by using script properties, scriptdb, a spreadsheet etc. See https://trello.com/docs/gettingstarted/authorize.html – Zig Mandel Nov 05 '13 at 03:17
  • OK, thanks. I'll give this a try. Your copy/paste solution is certainly sounding easier at this point ;o) Still frustrated with the fact that the Trello API doesn't support write access through OAuth thought. That would make things MUCH easier. – dixkin Nov 05 '13 at 04:44
  • cant edit my comment above, but I meant to say "This will CALL that URL along...". also, its not that trello doesnt support writes with oauth, it does. Its that its not compatible with oauthConfig which is oauth1. Its not hard to do it as I suggested, but it does have a drawback because in gas you cant show a popup or redirect (but I havent tried it with htmlService which might do it), so the user has to navigate to a second page (the redirect page that receives the token) – Zig Mandel Nov 05 '13 at 14:19
  • The following does not work. I am getting "returned code 404. Truncated server response: Cannot POST" function comment() { var url = "https://trello.com/1/cards/" + cardID + "/actions/comments&key=" + consumerKey + "&token=" + token + "&text=Testing..."; var postOptions = {"method" : "post", "token" : token, "key" : consumerKey}; var response = UrlFetchApp.fetch(url, postOptions); Logger.log(response.getContentText()); } – dixkin Nov 13 '13 at 15:47
  • Dont use the post options just method post. you already have the token and keys in the url. – Zig Mandel Nov 13 '13 at 16:38
  • That didn't work. I got the same error (cannot POST). I verified that the token I got has read/write permissions. – dixkin Nov 14 '13 at 18:43
  • I will post my code here soon. remind me if I dont. it does work :) – Zig Mandel Nov 16 '13 at 01:26
  • looking forward to it ;) – dixkin Nov 16 '13 at 01:30
  • here you go: 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); } – Zig Mandel Nov 16 '13 at 13:39
  • @ZigMandel, I was hoping you could cast your mind back to this: you say "To avoid the user copy/paste the token during your setup use the params callback_method" (fragment) and "return_url" to be gas_service_url. This will cause that URL along with "token=thetoken"), and you can handle that request and store it globally." I've got that bit to work but the token is passed after a # rather than as a query string: ...XXXX/exec#token=xxxxx... which isn't then passed in the event to the doGet(). Have you made it work with this technique? – Andrew Roberts Oct 08 '15 at 09:14
  • Sorry no i havent. Indeed a server cantón ser fragmentos. Hace you tried using htmlService to read the fragment client-side? – Zig Mandel Oct 08 '15 at 13:29
  • Meant "Cannot see fragments" – Zig Mandel Oct 10 '15 at 02:00