authlib icon indicating copy to clipboard operation
authlib copied to clipboard

Supplying api_base_url to OAuth2Session client has no effect

Open rorour opened this issue 2 years ago • 3 comments

Describe the bug

When instantiating a new authlib.integrations.requests_client.OAuth2Session, and supplying api_base_url to the constructor, api_base_url isn't used when making requests (ex client.get('some/resource').)

Error Stacks

File "github_oauth_test.py", line 20, in auth_github_authorized
    data = client.get('user').json()
  File "/home/vagrant/.pyenv/versions/py3/lib/python3.7/site-packages/requests/sessions.py", line 542, in get
    return self.request('GET', url, **kwargs)
  File "/home/vagrant/.pyenv/versions/py3/lib/python3.7/site-packages/authlib/integrations/requests_client/oauth2_session.py", line 105, in request
    method, url, auth=auth, **kwargs)
  File "/home/vagrant/.pyenv/versions/py3/lib/python3.7/site-packages/requests/sessions.py", line 515, in request
    prep = self.prepare_request(req)
  File "/home/vagrant/.pyenv/versions/py3/lib/python3.7/site-packages/requests/sessions.py", line 453, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/home/vagrant/.pyenv/versions/py3/lib/python3.7/site-packages/requests/models.py", line 318, in prepare
    self.prepare_url(url, params)
  File "/home/vagrant/.pyenv/versions/py3/lib/python3.7/site-packages/requests/models.py", line 392, in prepare_url
    raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL 'user': No scheme supplied. Perhaps you meant http://user/?

To Reproduce We know there is a Flask OAuth client, and our example below doesn't use it, but uses Flask to create an easy, reproducible example. In our real app, we are using OAuth2Session client and not using Flask.

import flask
import authlib.integrations.requests_client

app = flask.Flask(__name__)

@app.route('/')
def index():
    client = _client()
    uri, _ = client.create_authorization_url(
        'https://github.com/login/oauth/authorize',
        '<your-server-url>:8000/auth-github-authorize'
    )
    return flask.redirect(uri)

@app.route('/auth-github-authorized')
def auth_github_authorized():
    client = _client()
    client.fetch_token(authorization_response=flask.request.url)
    # FIXME: I should join api_base_url with user
    data = client.get('user').json()
    print(data)


def _client(state=None):
    return authlib.integrations.requests_client.OAuth2Session(
        # '<your-github-oauth-key>',
        # '<you-github-oauth-secret>',
        api_base_ur='https://api.github.com/',
        scope='user:email',
        state=state,
        token_endpoint='https://github.com/login/oauth/access_token',
    )

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

Expected behavior

api_base_url is joined with user in the call to client.get().

Environment:

  • OS: Fedora 32
  • Python Version: 3.7.2
  • Authlib Version: 1.0.0

rorour avatar Mar 22 '22 16:03 rorour

I'm happy to submit a patch if you agree that this is a bug.

rorour avatar Mar 22 '22 16:03 rorour

There is no api_base_url in OAuth2Session. api_base_url is defined for Flask, Django frameworks: https://docs.authlib.org/en/latest/client/frameworks.html

lepture avatar Mar 28 '22 02:03 lepture

This is more of a feature request, but it would be nice to have api_base_url for OAuth2Session as well.

rorour avatar Mar 28 '22 16:03 rorour