Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAuth2Session: Provide a default token_updater that just replaces self.token #411

Open
smarie opened this issue Apr 15, 2020 · 2 comments · May be fixed by #487
Open

OAuth2Session: Provide a default token_updater that just replaces self.token #411

smarie opened this issue Apr 15, 2020 · 2 comments · May be fixed by #487

Comments

@smarie
Copy link

smarie commented Apr 15, 2020

To make the auto-refresh procedure work with our oauth2 server I discovered that I had to create the token_updater function myself, and that it was this very simple one-liner:

oauth_session = OAuth2Session(client=my_oauth_client,  # the client knows the scope already
                              auto_refresh_url=refresh_token_url,
                              auto_refresh_kwargs={'client_id': client_id,
                                                   'target': client_id,
                                                   'api_type': refresh_api_type},
                              # Until PR is accepted the scope need to be repeated
                              scope=scopes  # https://github.com/requests/requests-oauthlib/pull/409
                              )

# HERE: could we get rid of this code by default ?
# the update procedure that will be called on successful token refresh
def token_updater(new_token):
    oauth_session.token = new_token
oauth_session.token_updater = token_updater

Am I right to think that the last three lines of code could be done automatically by default ?

If so I can propose a PR

@smarie smarie changed the title Provide a default token_updater that just replaces Provide a default token_updater that just replaces self.token Apr 15, 2020
@smarie smarie changed the title Provide a default token_updater that just replaces self.token OAuth2Session: Provide a default token_updater that just replaces self.token Apr 15, 2020
@Mike-Nahmias
Copy link

I think a default updater would be beneficial. I think you'll also need to set access_token. Maybe something like this within the OAuth2Session class:

def token_updater(self, token):
    self.token = token
    self.access_token = token['access_token']

@hiporox
Copy link

hiporox commented Mar 17, 2022

I think the issue is that client updates only happen if there is a token updater set (see https://github.com/requests/requests-oauthlib/blob/master/requests_oauthlib/oauth2_session.py#L537). If we move the self._client.add_token call outside of the if check, defining a default token_updater won't be necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants