我正在尝试学习如何将 OAuth 用于我正在开发的应用程序,但我似乎无法理解验证码部分和重定向 URI 的点。这就是我所拥有的......我不明白重定向URI的意义,一旦我得到一个有效的验证码。我什至如何以 JSON 格式为用户从 Google 中提取日历事件?
from pyoauth2 import Client
CLIENT_ID = 'XXXXXXXXXXXXXX'
CLIENT_SECRET = 'XXXXXXXXXXXXXX'
REDIRECT_URL = 'XXXXXXXXXXXXXX'
SCOPE = 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email'
client = Client(CLIENT_ID, CLIENT_SECRET,
site='https://www.googleapis.com/oauth2/v1',
authorize_url='https://accounts.google.com/o/oauth2/auth',
token_url='https://accounts.google.com/o/oauth2/token')
print '-' * 80
authorize_url = client.auth_code.authorize_url(redirect_uri=REDIRECT_URL, scope=SCOPE)
print 'Go to the following link in your browser:'
print authorize_url
code = raw_input('Enter the verification code and hit ENTER when you re done:')
code = code.strip()
access_token = client.auth_code.get_token(code, redirect_uri=REDIRECT_URL)
print 'token', access_token.headers
print '-' * 80
print 'get user info'
ret = access_token.get('/userinfo')
print ret.parsed
哎呀..忘记了错误..我什至不确定验证码应该是什么样子。
token {'Authorization': 'Bearer '}
--------------------------------------------------------------------------------
get user info
{u'error': {u'code': 401, u'message': u'Invalid Credentials', u'errors': [{u'locationType': u'header', u'domain': u'global', u'message': u'Invalid Credentials', u'reason': u'authError', u'location': u'Authorization'}]}}