Buduję stronę internetową + backend z FLask Framework, w której używam Flask-OAuthlib do uwierzytelniania za pomocą google. Po uwierzytelnieniu backend musi regularnie skanować użytkownika swoim Gmailem. Obecnie użytkownicy mogą uwierzytelniać moją aplikację i przechowywać access_token
i refresh_token
. access_token
wygasa po jednej godzinie, więc w ciągu tej jednej godziny można uzyskać userinfo tak:Jak korzystać z refresh_token, aby uzyskać nowy access_token (za pomocą Flask-OAuthLib)?
google = oauthManager.remote_app(
'google',
consumer_key='xxxxxxxxx.apps.googleusercontent.com',
consumer_secret='xxxxxxxxx',
request_token_params={
'scope': ['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/gmail.readonly'],
'access_type': 'offline'
},
base_url='https://www.googleapis.com/oauth2/v1/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://accounts.google.com/o/oauth2/token',
authorize_url='https://accounts.google.com/o/oauth2/auth'
)
token = (the_stored_access_token, '')
userinfoObj = google.get('userinfo', token=token).data
userinfoObj['id'] # Prints out my google id
Gdy godzin dobiegnie końca, muszę użyć refresh_token (który mam zapisany w mojej bazy danych) aby poprosić o nowy access_token
. Próbowałem zastąpić the_stored_access_token
z the_stored_refresh_token
, ale to po prostu daje mi Invalid Credentials
-error.
W this github issue czytam co następuje:
niezależnie od tego, w jaki sposób uzyskał dostęp tokena/odświeżania tokena (czy to poprzez dotację kod autoryzacji lub zasób haseł właściciel mandatów), wymieniać je w ten sam sposób, przez przekazanie odświeżającego tokenu jako refresh_token i grant_type ustawionego na "refresh_token".
Z tego zrozumiałem, musiałem stworzyć zdalną aplikację tak:
google = oauthManager.remote_app(
'google',
# also the consumer_key, secret, request_token_params, etc..
grant_type='refresh_token',
refresh_token=u'1/xK_ZIeFn9quwvk4t5VRtE2oYe5yxkRDbP9BQ99NcJT0'
)
Ale to prowadzi do TypeError: __init__() got an unexpected keyword argument 'refresh_token'
. Odtąd jestem trochę zagubiony.
Czy ktoś wie, jak mogę użyć refresh_token
, aby uzyskać nowy access_token
? Wszystkie wskazówki są mile widziane!