我正在将谷歌 xoauth.py 脚本集成到我的 django 应用程序中,因为我需要从我的应用程序访问用户的 Gmail 邮件。我的目的是在用户打算访问此功能并存储授权令牌以供以后使用后创建一个密钥/机密对。这是一个两步操作:获取验证 URL(用户在其中获取验证码)并获取授权令牌。第二步对我来说失败,并显示以下消息:
Request Method: GET
Request URL: http://localhost:8000/authorization_token?user=iferca@ecoresol.com&code=ycH19iaufCHoJ7APVkBGe_Hu&key=4/nj5s6sLBx-DKWUYJlUzERnngOO_6&token=wwvSU3ws448HR1gqiN7NxTh7
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/iferca/devel/gmail/workspace/imapsrv/imapsrv/../imapsrv/gdg/views.py" in request_authoization_token
27. response = xoauth.generateAuthorizationTokenFromVerificationCode(user, code, request_key, request_secret)
File "/Users/iferca/devel/gmail/workspace/imapsrv/imapsrv/../imapsrv/gdg/xoauth.py" in generateAuthorizationTokenFromVerificationCode
512. google_accounts_url_generator)
File "/Users/iferca/devel/gmail/workspace/imapsrv/imapsrv/../imapsrv/gdg/xoauth.py" in GetAccessToken
316. request_token.secret)
File "/Users/iferca/devel/gmail/workspace/imapsrv/imapsrv/../imapsrv/gdg/xoauth.py" in GenerateOauthSignature
200. return GenerateHmacSha1Signature(base_string, key)
File "/Users/iferca/devel/gmail/workspace/imapsrv/imapsrv/../imapsrv/gdg/xoauth.py" in GenerateHmacSha1Signature
194. digest = hmac.new(key, text, hashlib.sha1)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hmac.py" in new
133. return HMAC(key, msg, digestmod)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hmac.py" in __init__
72. self.outer.update(key.translate(trans_5C))
Exception Type: TypeError at /authorization_token
Exception Value: character mapping must return integer, None or unicode
相同的脚本在命令行中运行良好。我什至使用 python 控制台测试了手动抛出错误的行,但我无法重现错误,如下所示:
>>> key = "anonymous&wwvSU3ws448HR1gqiN7NxTh7"
>>> trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
>>> trans_5C
'\]^_XYZ[TUVWPQRSLMNOHIJKDEFG@ABC|}~x7fxyz{tuvwpqrslmnohijkdefg`abcx1cx1dx1ex1fx18x19x1ax1bx14x15x16x17x10x11x12x13x0crx0ex0fx08tnx0bx04x05x06x07x00x01x02x03<=>?89:;45670123,-./()*+$%&' !"#xdcxddxdexdfxd8xd9xdaxdbxd4xd5xd6xd7xd0xd1xd2xd3xccxcdxcexcfxc8xc9xcaxcbxc4xc5xc6xc7xc0xc1xc2xc3xfcxfdxfexffxf8xf9xfaxfbxf4xf5xf6xf7xf0xf1xf2xf3xecxedxeexefxe8xe9xeaxebxe4xe5xe6xe7xe0xe1xe2xe3x9cx9dx9ex9fx98x99x9ax9bx94x95x96x97x90x91x92x93x8cx8dx8ex8fx88x89x8ax8bx84x85x86x87x80x81x82x83xbcxbdxbexbfxb8xb9xbaxbbxb4xb5xb6xb7xb0xb1xb2xb3xacxadxaexafxa8xa9xaaxabxa4xa5xa6xa7xa0xa1xa2xa3'
>>> key.translate(trans_5C)
'=232%13)/z++*x0fto+/hhdx14x0em;-5x12kx12$x084k'
我在脚本中添加了两个函数,供我的 Web 应用程序用作两个步骤的接口,函数如下:
def requestTokenVerificationURL(user):
if not user:
raise Exception('requestTokenVerificationURL invoked without user argument')
scope = 'https://mail.google.com/'
nonce = None
timestamp = None
consumer_key = 'anonymous'
consumer_secret = 'anonymous'
consumer = OAuthEntity(consumer_key, consumer_secret)
google_accounts_url_generator = GoogleAccountsUrlGenerator(user)
authurl_token = GenerateRequestTokenAndAuthorizationURL(consumer, scope, nonce,
timestamp, google_accounts_url_generator)
return {'request_token_key': authurl_token['token'].key,
'request_token_secret': authurl_token['token'].secret,
'authorization_url': authurl_token['authorization_url'].strip()}
def generateAuthorizationTokenFromVerificationCode(user, verificationCode, request_key, request_secret):
consumer_key = 'anonymous'
consumer_secret = 'anonymous'
consumer = OAuthEntity(consumer_key, consumer_secret)
request_token = OAuthEntity(request_key, request_secret)
google_accounts_url_generator = GoogleAccountsUrlGenerator(user)
access_token = GetAccessToken(consumer, request_token, verificationCode,
google_accounts_url_generator)
return access_token
任何命中将不胜感激,提前感谢
我发现了问题!
这个问题与请求令牌和密钥来回传递给客户端的事实有关,并且在从GET或POST请求获取它的过程中,django返回一个Unicode而不是一个字符串(str)。此问题已通过将 unicode 转换为字符串请求令牌/机密,然后再将它们传递给 xoauth.py 函数来解决。
希望这将节省 3 天的测试时间给其他人!