google - trello power ups




How to authorize and post/update Trello card from a Google docs script (5)

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.


If you have to do everything server-side, Andy Jones is correct, those are the only two ways.

It should be noted, however, that if you can write javascript+jquery code rather than having to do the redirections server-side, you can take advantage of Trello's client.js wrapper, which does exactly what Andy described, but takes care of most of it for you, which is way convenient.

And, as I recently discovered, if you do need to do processing server-side, you can still probably use client.js, then just get the token with Trello.token() in your auth success handler, and pass that to your server-side code. It looks like this:

// include whatever version of jquery you want to use first
<script src="https://api.trello.com/1/client.js?key=[your application key]" type="text/javascript"></script>

// call this whenever you want to make sure Trello is authenticated, and get a key. 
// I don't call it until the user needs to push something to Trello,
// but you could call it in document.ready if that made more sense in your case.
function AuthenticateTrello() {
        Trello.authorize({
            name: "your project name",
            type: "popup",
            interactive: true,
            expiration: "never",
            success: function () { onAuthorizeSuccessful(); },
            error: function () { onFailedAuthorization(); },
            scope: { write: true, read: true },
        });
 }

function onAuthorizeSuccessful() {
    var token = Trello.token();
    // whatever you want to do with your token. 
    // if you can do everything client-side, there are other wrapper functions
    // so you never need to use the token directly if you don't want to.
}

function onFailedAuthorization() {
    // whatever
}

If you only need a token for personal use you can get app-key, secret and token based on you being logged in over here.


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

Google apps script oauth connect doesn't work with trello

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

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

Using the sendHttpPost example from the docs for UrlFetchApp and the docs for the Trello API, I came up with this:

 // This sample sends POST payload data in the style of an HTML form, including
 // a file.

 function createTrelloCard() {

   //POST [/1/cards], Required permissions: write
   var payload = {"name":"apiUploadedCard", //(required) Valid Values: a string with a length from 1 to 16384
                  "desc":"description", //(optional)Valid Values: a string with a length from 0 to 16384
                  "pos":"top", //(optional) Default: bottom Valid Values: A position. top, bottom, or a positive number.
                  "due": "", //(required) Valid Values: A date, or null
                  "idList":"52017776e823fa1d51000819", //(required)Valid Values: id of the list that the card should be added to
                  //"labels": ,//(optional)
                  //"idMembers": ,//(optional)Valid Values: A comma-separated list of objectIds, 24-character hex strings
                  //"idCardSource": ,//(optional)Valid Values: The id of the card to copy into a new card.
                  //"keepFromSource": ,//(optional)Default: all Valid Values: Properties of the card to copy over from the source.
                 };

   // Because payload is a JavaScript object, it will be interpreted as
   // an HTML form. (We do not need to specify contentType; it will
   // automatically default to either 'application/x-www-form-urlencoded'
   // or 'multipart/form-data')
   var url = 'https://api.trello.com/1/cards?key=[YourAppKey]&token=[UserToken]' //optional... -&cards=open&lists=open'-
   var options = {"method" : "post",
                  "payload" : payload};

   UrlFetchApp.fetch(url, options);
 }

Disclaimer: I haven't tested this. I've never written a Google App script or used the Trello API.