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

Refreshing the token after loading it from file #241

Closed
NotaLabs opened this issue Jun 30, 2016 · 6 comments
Closed

Refreshing the token after loading it from file #241

NotaLabs opened this issue Jun 30, 2016 · 6 comments

Comments

@NotaLabs
Copy link

Hello,
i tried to configure my oauthlib the way, that it automaticly refreshes the token, unfortanatly i can`t seem to get it right after loading my token from a file.

import pickle
import os.path
from time import time

# Credentials you get from registering a new application
client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'
client_secret = 'xxxxxxxxxxxxxxxxx'
redirect_uri = 'http:https://localhost'

# OAuth endpoints given in the Google API documentation
authorization_base_url = "https://accounts.google.com/o/oauth2/v2/auth"
token_url = "https://www.googleapis.com/oauth2/v4/token"
scope = [
    "https://picasaweb.google.com/data/",
]

extra = {
    'client_id': client_id,
    'client_secret': client_secret,
}

from requests_oauthlib import OAuth2Session

if not os.path.isfile("token.p"):

    google = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri, auto_refresh_url=token_url,auto_refresh_kwargs=extra)


    authorization_url, state = google.authorization_url(authorization_base_url, access_type="offline")
    print('Please go here and authorize,', authorization_url)


    redirect_response = input('Paste the full redirect URL here:')


    google.fetch_token(token_url, client_secret=client_secret,
                       authorization_response=redirect_response)


    pickle.dump(google.token, open("token.p", "wb"))
else:
    gtoken = pickle.load(open("token.p", "rb"))

    #Forcing the token to expire
    gtoken['expires_at'] = time() - 10

    google = OAuth2Session(client_id, token=gtoken)

    #manually refreshing works!
    #client.token = client.refresh_token(token_url, **extra)

google.get('https://www.googleapis.com/oauth2/v1/userinfo')

But it just produces the following error:

C:\Users\Tim\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/Tim/PycharmProjects/copy+/googleconnect.py
Traceback (most recent call last):
  File "C:/Users/Tim/PycharmProjects/copy+/googleconnect.py", line 50, in <module>
    google.get('https://www.googleapis.com/oauth2/v1/userinfo')
  File "C:\Users\Tim\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\sessions.py", line 487, in get
    return self.request('GET', url, **kwargs)
  File "C:\Users\Tim\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests_oauthlib\oauth2_session.py", line 321, in request
    http_method=method, body=data, headers=headers)
  File "C:\Users\Tim\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 194, in add_token
    raise TokenExpiredError()
oauthlib.oauth2.rfc6749.errors.TokenExpiredError: (token_expired) 

Process finished with exit code 1

@Lukasa
Copy link
Member

Lukasa commented Jul 1, 2016

In your second branch of code, you don't set the auto_refresh_url or auto_refresh_kwargs, so requests-oauthlib cannot refresh your token for you. Try setting those when you create your OAuth2Session.

@NotaLabs
Copy link
Author

NotaLabs commented Jul 1, 2016

I updated my code:

gtoken = pickle.load(open("token.p", "rb"))

    #Forcing the token to expire
    gtoken['expires_at'] = time() - 10

    google = OAuth2Session(client_id, token=gtoken, auto_refresh_url=token_url, auto_refresh_kwargs=extra)

    #google.token = google.refresh_token(token_url, **extra)

    google.get('https://www.googleapis.com/oauth2/v1/userinfo')

r = google.get('https://picasaweb.google.com/data/feed/api/user/default')
#print(r.text)
print(r.status_code)

It still doesn`t seem to work, throwing the following exception:

oauthlib.oauth2.rfc6749.errors.TokenExpiredError

@Lukasa
Copy link
Member

Lukasa commented Jul 1, 2016

Can you provide the full traceback for that exception?

@NotaLabs
Copy link
Author

NotaLabs commented Jul 1, 2016

Traceback (most recent call last):
File "C:\Users\Tim\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests_oauthlib\oauth2_session.py", line 321, in request
http_method=method, body=data, headers=headers)
File "C:\Users\Tim\AppData\Local\Programs\Python\Python35-32\lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 194, in add_token
raise TokenExpiredError()
oauthlib.oauth2.rfc6749.errors.TokenExpiredError: (token_expired)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/Tim/PycharmProjects/copy+/googleconnect.py", line 53, in
google.get('https://www.googleapis.com/oauth2/v1/userinfo')
File "C:\Users\Tim\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\sessions.py", line 487, in get
return self.request('GET', url, **kwargs)
File "C:\Users\Tim\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests_oauthlib\oauth2_session.py", line 335, in request
raise TokenUpdated(token)
requests_oauthlib.oauth2_session.TokenUpdated

@Lukasa
Copy link
Member

Lukasa commented Jul 1, 2016

So the actual exception you're now seeing is TokenUpdated. This is exactly as expected: you have no token_updater method, so we raise this to tell you that the token got updated.

Now, looking at the code (I didn't write this section), I feel like this was intended to be an actual warning, rather than an exception. However, in your case, you should provide a token updater: you're persisting tokens out to file, so if they change, you should make sure you update the file appropriately.

@NotaLabs
Copy link
Author

NotaLabs commented Jul 1, 2016

Thank you,
i didn`t know it was necessary to write a token updater.

@NotaLabs NotaLabs closed this as completed Jul 1, 2016
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

No branches or pull requests

2 participants