我正在尝试按照以下流程使用 python 服务器后端在 Android 应用程序上登录用户:
https://developers.google.com/+/web/signin/server-side-flow https://developers.google.com/+/web/signin/server-side-flow
我成功地从 Android 应用程序获取了授权代码,但是当我尝试将此代码交换为来自服务器的访问令牌时,我收到“invalid_request”错误。
在 Android 应用程序中,我使用与服务器上的 client_id 相同的 client_id,该 client_id 在我的控制台中的“Web 应用程序的客户端 ID”下列出。我已经验证了redirect_uri 是正确的。难道不能从Android客户端生成授权码并使用服务器来交换访问令牌吗?
我的Python代码是:
def auth_params(self):
client_id, client_secret = self.get_key_and_secret()
return {
'grant_type': 'authorization_code',
'code': self.data.get('code', ''), # auth code from app
'client_id': client_id,
'client_secret': client_secret,
'redirect_uri': self.get_redirect_uri()
}
@classmethod
def auth_headers(cls):
return {'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'}
def auth_complete(self, *args, **kwargs):
params = self.auth_params()
request = Request('https://accounts.google.com/o/oauth2/token', data=urlencode(params),
headers=self.auth_headers())
try:
response = simplejson.loads(urlopen(request).read())
except HTTPError, e:
print 'fml'