【发布时间】:2016-04-20 09:49:11
【问题描述】:
早安,
我在编写用于登录我的网络应用程序(托管在 gae 中)的回调方法时遇到了一些麻烦
@app.route('/callback')
def callback_handling():
env = os.environ
code = request.args.get('code')
json_header={'Content-Type': 'application/x-www-form-urlencoded'}
token_url = "https://{domain}/oauth/token".format(domain='mydomain')
token_payload = {
'client_id': 'myid',
'client_secret': 'mysecret',
'redirect_uri': 'http://localhost:8080/callback',
'code': code,
'grant_type': 'authorization_code'
}
encoded = urllib.urlencode(token_payload)
user_url = "https://{domain}/userinfo?access_token={access_token}"\
.format(access_token=token_info['access_token'])
user_info = urlfetch.Fetch(user_url, method=urlfetch.GET, headers=json_header)
session['profile'] = user_info
return redirect('/dashboard')
我经常得到错误 401 unathorized..从调试控制台有什么想法可以修复?..通过互联网阅读我知道我必须使用 gae 的 urlfetch,因为它是 gae 中唯一允许的库(请求不在 localhost 中)。 .
【问题讨论】:
-
缺少一段代码:token_info = urlfetch.fetch(url=token_url, payload=encoded, headers={'Content-Type': 'application/x-www-form-urlencoded'} , 方法=urlfetch.POST)