我正在尝试使用Google Calendar API授权Oauth2。我猜我的请求有问题,因为我收到redirect_uri mismatch
错误。
我正在使用重定向uri urn:ietf:wg:oauth:2.0:oob
,这是我Google Developer console项目中的原生应用程序。
登录页面将用户重定向到Googles授权页面,而/ oauth2callback将使用生成的Google代码从提交的表单中获取信息。 代码是
@app.route('/login')
def login():
flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope='https://www.googleapis.com/auth/calendar',
redirect_uri='urn:ietf:wg:oauth:2.0:oob',
approval_prompt='force',
access_type='offline')
auth_uri = flow.step1_get_authorize_url()
return redirect(auth_uri)
@app.route('/oauth2callback', methods=['POST', 'GET'])
def oauth2callback():
code = request.form['code']
if code:
# exchange the authorization code for user credentials
flow = OAuth2WebServerFlow(CLIENT_ID,
CLIENT_SECRET,
scope="https://www.googleapis.com/auth/calendar",
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
flow.redirect_uri = request.base_url
try:
credentials = flow.step2_exchange(code)
storage = Storage('credentials.dat')
storage.put(credentials)
except Exception as e:
logging.warning("Unable to get an access token because "+e.message)
return redirect('/')