0

我正在将来自 google 的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`abc\x1c\x1d\x1e\x1f\x18\x19\x1a\x1b\x14\x15\x16\x17\x10\x11\x12\x13\x0c\r\x0e\x0f\x08\t\n\x0b\x04\x05\x06\x07\x00\x01\x02\x03<=>?89:;45670123,-./()*+$%&\' !"#\xdc\xdd\xde\xdf\xd8\xd9\xda\xdb\xd4\xd5\xd6\xd7\xd0\xd1\xd2\xd3\xcc\xcd\xce\xcf\xc8\xc9\xca\xcb\xc4\xc5\xc6\xc7\xc0\xc1\xc2\xc3\xfc\xfd\xfe\xff\xf8\xf9\xfa\xfb\xf4\xf5\xf6\xf7\xf0\xf1\xf2\xf3\xec\xed\xee\xef\xe8\xe9\xea\xeb\xe4\xe5\xe6\xe7\xe0\xe1\xe2\xe3\x9c\x9d\x9e\x9f\x98\x99\x9a\x9b\x94\x95\x96\x97\x90\x91\x92\x93\x8c\x8d\x8e\x8f\x88\x89\x8a\x8b\x84\x85\x86\x87\x80\x81\x82\x83\xbc\xbd\xbe\xbf\xb8\xb9\xba\xbb\xb4\xb5\xb6\xb7\xb0\xb1\xb2\xb3\xac\xad\xae\xaf\xa8\xa9\xaa\xab\xa4\xa5\xa6\xa7\xa0\xa1\xa2\xa3'
>>> key.translate(trans_5C)
'=232%13)/z++*\x0f\to+/hhd\x14\x0em;-5\x12k\x12$\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

任何命中将不胜感激,提前

4

1 回答 1

0

我发现问题了!

该问题与请求令牌和秘密来回传递给客户端以及在从 GET 或 POST 请求中获取它的过程中 django 返回一个 unicode 而不是字符串 (str) 的事实有关。通过在将请求令牌/密钥传递给 xoauth.py 函数之前将其转换为字符串,解决了该问题。

希望这可以为其他人节省 3 天的测试时间!

于 2013-01-30T16:37:02.817 回答