Ich verwende OAuthlib, um den OAuth-Flow von Google durchzuführen. Es hat 4 bis 5 Monate gut funktioniert. Plötzlich fing ich an, unter Fehler zu kommen:
File "/home/whitesnow-2/Gaurav/Axonator/AxVirtualEnv/local/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py",
line 409, in validate_token_parameters raise w Warning: Scope has changed from
"https://www.googleapis.com/auth/calendar
https://www.googleapis.com/auth/docs
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile" to
"https://www.googleapis.com/auth/calendar
https://www.googleapis.com/auth/docs
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile".
Unten finden Sie den Code zum Generieren der OAuth-Autorisierungs-URL:
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
scopes=['https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/docs https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'],
redirect_uri=REDIRECT_URI
)
authorization_url, state = flow.authorization_url(
access_type='offline',
include_granted_scopes='true',
Prompt='consent'
)
Nachfolgend finden Sie den Code für den Google OAuth-Rückruf:
auth_code = request.GET.get("code")
objectid = request.GET.get("state")
error = request.GET.get("error")
if error == "access_denied":
return "Access Denied"
else:
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
scopes=['https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/docs https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'],
redirect_uri=REDIRECT_URI
)
flow.fetch_token(code=auth_code)
Sie können diese Warnung deaktivieren, indem Sie die Umgebungsvariable OAUTHLIB_RELAX_TOKEN_SCOPE
festlegen. Dies sollte in Fällen funktionieren, in denen Sie den Code, der die oauth-Bibliothek aufruft, nicht steuern.
Folgendes ist in der oauthlib-Bibliothek implementiert:
https://github.com/oauthlib/oauthlib/blob/master/oauthlib/oauth2/rfc6749/parameters.py#L401
Ich habe den Bereich https://www.googleapis.com/auth/plus.me
hinzugefügt, in dem ich meine Flow
-Objekte erstelle:
Flow.from_client_config(
secrets_json_string,
scopes=[
(…),
'https://www.googleapis.com/auth/plus.me',
],
redirect_uri=redirect_url
)
Sogar ich hatte auch das gleiche Problem. Ich habe dies durch Entfernen von include_granted_scopes='true',
in der flow.authorization_url behoben
Ich konnte das Problem umgehen, indem ich die Bereiche in der Rückruffunktion auf None
stellte.
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
scopes=None,
redirect_uri=REDIRECT_URI
)
flow.fetch_token(code=auth_code)