How to get a permanent user token for writes using the Trello API?

廉价感情. 提交于 2019-11-29 20:17:44

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!