Help with automatically getting a token for Trello using Flask and Python

Dara O hEidhin February 25, 2024

Hi,
I'm trying to create an internal app to automate Trello and can get a token using the API key if I manually copy and paste from the Trello response.

I wish to automate this using a temporary Flask server. The issue I'm having is that after the authorization callback fires the token is embedded in the redirected URL and I can't seem to find a way to extract it. Any help would be appreciated.

Note the url in the browser after the callback is in the format:
http://127.0.0.1:5000/callback#token=<MY TOKEN with 76 characters>

I can't find any way to save/store the token using code and I can't seem to find a way to programmatically access the url in the browser to extract the token characters.

Below is my code. Note that I'm not that familiar with OAuth


``` python

from dataclasses import dataclass

import keyring
from flask import Flask, jsonify, redirect, request, session, url_for
from flask_oauthlib.contrib.client import OAuth
from flask_session import Session
import secrets

from skunkautomation.shared import get_password as get_api_key

app = Flask(__name__)

app.testing = True
app.config["SESSION_TYPE"] = "memcached"
app.config["SECRET_KEY"] = secrets.token_urlsafe(16)


RETURN_URL = "http%3A%2F%2Flocalhost%3A5000%2F"
AUTH_URL = "&".join(
    [
        "scope=read",
        "response_type=token",
        f"key={get_api_key()}",
        f"return_url={RETURN_URL}",
        "response_type=token",
    ]
)



Session(app)
oauth = OAuth(app)
trello = oauth.remote_app(
    name="trello",
    version="2",
    authorization_url=AUTH_URL,
    access_token_url=TOKEN_URL,
    client_id=None,
    client_secret=None,
)


@app.route("/")
def home():
    # Check if token is already stored
    token = None  # get_stored_token()
    if token:
        # Use the stored token for API calls
        return "You have a token!"
    else:
        # Initiate the authorization flow
        redirect_url = url_for("oauth_callback", _external=True)
        response = trello.authorize(callback_uri=redirect_url)
        return response


@app.route("/callback")
def oauth_callback():
    # TODO extract token
   
    return "blah"  # "Token retrieved successfully!"


if __name__ == "__main__":
    app.run(port=5000)
```

I'm getting an email from Attlassian every time I run the code to say "We noticed that you’ve authorized a new application in your Trello account." and if I manually copy the token embedded in the browser URL I can return a list of boards using the code below


```python
from trello import TrelloClient

client = TrelloClient(
    api_key=password,
    token='<TOKEN copied from callback url>',
)

client.list_boards()

# This works
```



1 answer

0 votes
Alina Kurishko
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 27, 2024

Hi Dara, 

This would be a really great question to share with our developer community. We have a number of Trello Engineers who are active there, and they'd be happy to give some guidance for more advanced topics like these.

Would you please report your question here: https://community.developer.atlassian.com/c/trello

Alina | The Trello Team

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events