How to get a permanent user token for writes using

2019-01-30 18:46发布

I'm trying to write an app that updates my Trello cards with the API. How do I get a permanent user token for the app to write to my Trello board?

Thanks

标签: trello
3条回答
forever°为你锁心
2楼-- · 2019-01-30 19:16

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.

查看更多
萌系小妹纸
3楼-- · 2019-01-30 19:19

You can do this in one of 2 ways -

Direct the user to the below address. This will direct the user to a page that has a token that she can copy and paste back to you. The important bit is that you ask for expiration = never and scope = read,write

https://trello.com/1/authorize?key=substitutewithyourapplicationkey&scope=read%2Cwrite&name=My+Application&expiration=never&response_type=token

Or use OAuth (harder) to automate the request for an access token. Read more in the documentation.

Once you have the token, you can make any API call you'd like.

查看更多
Anthone
4楼-- · 2019-01-30 19:27

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
}
查看更多
登录 后发表回答