Search code examples
python-3.xflaskoauth-2.0authlib

How to fix 'function' object has no attribute 'create_authorization_url' when using Authlib?


I'm writing an API that connects to Budget Insight https://www.budget-insight.com/ to fetch financial data from end users.

Budget Insight uses Oauth2 authentication on a webview where user is asked to enter his credentials to generate a code that is then exchanged for the token.

I have installed authlib and its dependencies for my project and tried different ways to make this work

First way

budgea = OAuth2Session(
    client_id="37246252",
    client_secret=os.getenv("BUDGEA_CLIENT_SECRET"),
    redirect_uri="http://127.0.0.1:5000/budgea_callback",
)

Second way

budgea = oauth2.register(
    "budgea",
    client_id="37246252",
    client_secret=os.getenv("BUDGEA_CLIENT_SECRET"),
    access_token_url="https://ynab-fr-sandbox.biapi.pro/2.0/auth/token/access",
    authorize_url="https://ynab-fr-sandbox.biapi.pro/2.0/auth/webview/connect/select?client_id=37246252&redirect_uri=http:%2F%2F127.0.0.1:5000%2Fbudgea_callback",
    api_base_url="https://ynab-fr-sandbox.biapi.pro/2.0/",
    client_kwargs=None,
)

followed by

@app.route("/budgea")
def budgea():
    authorize_url = "https://ynab-fr-sandbox.biapi.pro/2.0/auth/webview/connect/select?client_id=37246252&redirect_uri=http:%2F%2F127.0.0.1:5000%2Fbudgea_callback"
    uri, state = budgea.create_authorization_url(authorize_url)
    return uri

Both ways return an AttributeError: 'function' object has no attribute 'create_authorization_url' when I visit the /budgea route.


Solution

  • What is the version of Authlib you are using? Please use the latest version of Authlib.

    1. Then try create_authorization_url with OAuth2Session way
    2. The second way is the Flask client way, you should use

      return client.authorize_redirect(callback_uri)