我正在遵循此示例,以尝试使用Google API对用户进行身份验证。
这是我正在使用的相关代码,几乎与示例中的代码完全一样:
@app.route('/login/')
def login():
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
'client_secret.json',
scopes=['https://www.googleapis.com/auth/drive.file'])
flow.redirect_uri = 'https://localhost:5000/oauth2callback/'
authorization_url, state = flow.authorization_url(
access_type='offline',
include_granted_scopes='true')
session['state'] = state
return redirect(authorization_url)
#called by Google's oauth
@app.route('/oauth2callback/')
def oauth2callback():
state = request.args['state']
# Use the client_secret.json file to identify the application requesting
# authorization. The client ID (from that file) and access scopes are required.
state = session['state']
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
'client_secret.json',
scopes=['https://www.googleapis.com/auth/drive.file'],
state=state)
flow.redirect_uri = url_for('oauth2callback', _external=True)
authorization_response = request.url
flow.fetch_token(authorization_response=authorization_response, code_verifier=False)
Run Code Online (Sandbox Code Playgroud)
当我这样做时,它给出了错误,
oauthlib.oauth2.rfc6749.errors.InvalidGrantError: …