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
```