2

I'm trying to build a Google Apps Script that integrates with Trello, the idea being to use it to push information from spreadsheets and forms into the Trello API and create cards on a pending list on a certain board.

I found another question that pointed me in the right direction, and added in OAuth based on the GAS OAuth Documentation. The problem is I can't post the the board. I run the script, the OAuth prompt fires, and the script completes with no errors. I can also GET data from the private board, so I assume the authorization is working properly.

So, what am I doing wrong that prevents my script from POSTing to Trello?

Here's the code I'm working with:

var trelloKey = [Trello API key];
var trelloSecret = [Trello API key secret];
var trelloList = [the id of the list we're posting to];

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(trelloKey);
    oauthConfig.setConsumerSecret(trelloSecret);

function createTrelloCard() {

  //POST [/1/cards], Required permissions: write
  var payload = {'name': 'apiUploadedCard', 
                 'desc': 'description', 
                 'pos': 'top', 
                 'due': '', 
                 'idList': trelloList};

  var url = 'https://api.trello.com/1/cards'
  var options = {'method' : 'post',
                 'payload' : payload,
                 'oAuthServiceName' : 'trello',
                 'oAuthUseToken' : 'always'};

  UrlFetchApp.fetch(url, options);
}
Community
  • 1
  • 1
justin
  • 1,528
  • 11
  • 26
  • see my answer on http://stackoverflow.com/questions/19597327/how-to-authorize-and-post-update-trello-card-from-a-google-docs-script/19646691 – Zig Mandel Nov 22 '13 at 23:40
  • @ZigMandel I'm not sure that your answer addresses my question. I'm looking for a reason why the code I'm using doesn't work. I'm also not looking to store any access tokens. One-offing the auth requests is better for what we're doing in this context. – justin Nov 23 '13 at 01:52
  • the gas oauth helpers might not work correctly with trello. You can however make your own urlfetch query and give it a gas callback from where you can store the token. Since you can request a non-expiring token in trello, the easiest is to have a GUI where the user enters such token or you collect it with a callback. then you just always use that same token and use urlfetch without oauth (just put the token stuff as url params) – Zig Mandel Nov 23 '13 at 02:02

2 Answers2

2

You just need set fetch options contentType to application/json. I just resolved the same problem by this.

Robert
  • 5,278
  • 43
  • 65
  • 115
Ulion
  • 21
  • 1
  • 2
1

Try adding the scope=read,write in your authorization url.

from:

oauthConfig.setAuthorizationUrl('https://trello.com/1/OAuthAuthorizeToken');

to:

oauthConfig.setAuthorizationUrl("https://trello.com/1/OAuthAuthorizeToken?scope=read,write");
Diogo Alves
  • 101
  • 4