Oauth使用Python / Django的Google API示例

我正试图让Oauth使用Python来处理Google API。 我尝试过不同的oauth库,比如oauth , oauth2和djanog-oauth,但是我不能使它工作(包括提供的例子)。

为了debuggingOauth,我使用Google的Oauth Playground ,并研究了API和Oauth文档

有些库我正在努力获得一个正确的签名,与其他图书馆我努力将请求令牌转换为授权令牌。 如果有人能够使用上述库中的一个向我展示Google API的工作示例,那么真的会有帮助。

编辑:我最初的问题没有导致任何答案,所以我已经添加了我的代码。 这个代码有两个可能的原因不起作用:
1)Google不会授权我的请求令牌,但不太确定如何检测到这一点
2)访问令牌的签名是无效的,但是我想知道Google所期望的oauth参数,因为我能够在第一阶段生成一个合适的签名。

这是使用oauth2.py和Django编写的,因此是HttpResponseRedirect。

REQUEST_TOKEN_URL = 'https://www.google.com/accounts/OAuthGetRequestToken' AUTHORIZATION_URL = 'https://www.google.com/accounts/OAuthAuthorizeToken' ACCESS_TOKEN_URL = 'https://www.google.com/accounts/OAuthGetAccessToken' CALLBACK = 'http://localhost:8000/mappr/mappr/oauth/' #will become real server when deployed OAUTH_CONSUMER_KEY = 'anonymous' OAUTH_CONSUMER_SECRET = 'anonymous' signature_method = oauth.SignatureMethod_HMAC_SHA1() consumer = oauth.Consumer(key=OAUTH_CONSUMER_KEY, secret=OAUTH_CONSUMER_SECRET) client = oauth.Client(consumer) request_token = oauth.Token('','') #hackish way to be able to access the token in different functions, I know this is bad, but I just want it to get working in the first place :) def authorize(request): if request.GET == {}: tokens = OAuthGetRequestToken() return HttpResponseRedirect(AUTHORIZATION_URL + '?' + tokens) elif request.GET['oauth_verifier'] != '': oauth_token = request.GET['oauth_token'] oauth_verifier = request.GET['oauth_verifier'] OAuthAuthorizeToken(oauth_token) OAuthGetAccessToken(oauth_token, oauth_verifier) #I need to add a Django return object but I am still debugging other phases. def OAuthGetRequestToken(): print '*** OUTPUT OAuthGetRequestToken ***' params = { 'oauth_consumer_key': OAUTH_CONSUMER_KEY, 'oauth_nonce': oauth.generate_nonce(), 'oauth_signature_method': 'HMAC-SHA1', 'oauth_timestamp': int(time.time()), #The timestamp should be expressed in number of seconds after January 1, 1970 00:00:00 GMT. 'scope': 'https://www.google.com/analytics/feeds/', 'oauth_callback': CALLBACK, 'oauth_version': '1.0' } # Sign the request. req = oauth.Request(method="GET", url=REQUEST_TOKEN_URL, parameters=params) req.sign_request(signature_method, consumer, None) tokens =client.request(req.to_url())[1] params = ConvertURLParamstoDictionary(tokens) request_token.key = params['oauth_token'] request_token.secret = params['oauth_token_secret'] return tokens def OAuthAuthorizeToken(oauth_token): print '*** OUTPUT OAuthAuthorizeToken ***' params ={ 'oauth_token' :oauth_token, 'hd': 'default' } req = oauth.Request(method="GET", url=AUTHORIZATION_URL, parameters=params) req.sign_request(signature_method, consumer, request_token) response =client.request(req.to_url()) print response #for debugging purposes def OAuthGetAccessToken(oauth_token, oauth_verifier): print '*** OUTPUT OAuthGetAccessToken ***' params = { 'oauth_consumer_key': OAUTH_CONSUMER_KEY, 'oauth_token': oauth_token, 'oauth_verifier': oauth_verifier, 'oauth_token_secret': request_token.secret, 'oauth_signature_method': 'HMAC-SHA1', 'oauth_timestamp': int(time.time()), 'oauth_nonce': oauth.generate_nonce(), 'oauth_version': '1.0', } req = oauth.Request(method="GET", url=ACCESS_TOKEN_URL, parameters=params) req.sign_request(signature_method, consumer, request_token) response =client.request(req.to_url()) print response return req def ConvertURLParamstoDictionary(tokens): params = {} tokens = tokens.split('&') for token in tokens: token = token.split('=') params[token[0]] = token[1] return params 

我有一个在Python App Engine应用程序中工作的OAuth:

http://github.com/sje397/Chess

该应用程序运行在:

http://your-move.appspot.com

这为我工作。

 def login(request): consumer_key = 'blabla' consumer_secret = 'blabla' callback = request.GET['callback'] request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken' authorize_url = 'https://api.linkedin.com/uas/oauth/authorize' access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken' consumer = oauth.Consumer(consumer_key, consumer_secret) if ('oauth_verifier' not in request.GET): client = oauth.Client(consumer) body = 'oauth_callback=http://shofin.com/login?callback='+callback+"&placeId="+request.GET[placeId] resp,content = client.request(request_token_url,"POST",headers={'Content-Type':'application/x-www-form-urlencoded'},body=body) request_token = dict(urlparse.parse_qsl(content)) loginUrl = authorize_url+"?oauth_token="+request_token['oauth_token'] cache.set(request_token['oauth_token'],request_token['oauth_token_secret']) return HttpResponseRedirect(loginUrl) elif request.GET['oauth_verifier']: token = oauth.Token(request.GET['oauth_token'],cache.get(request.GET['oauth_token'])) token.set_verifier(request.GET['oauth_verifier']) client = oauth.Client(consumer, token) resp,content = client.request(access_token_url,"POST",{}) access_token = dict(urlparse.parse_qsl(content)) token = oauth.Token(key=access_token['oauth_token'], secret=access_token['oauth_token_secret']) client = oauth.Client(consumer, token) resp,json = client.request("http://api.linkedin.com/v1/people/~?format=json") return render_to_response(callback,{'placeId':request.GET['placeId'],'userId':userId,'folkId':folkId) 

你有没有尝试过官方的gdata python api? 它附带一个oauth客户端,并隐藏oauth调用的复杂性。 http://code.google.com/p/gdata-python-client/

这可能是答案。

当调用OAuthGetRequestToken时,你用你的consumer_secret签署了base_string,后跟一个&(&符号)

当调用OAuthGetAccessToken时,您使用您的consumer_secret签署base_string,后跟&(&符号),然后是token_secret。

您可以使用(consumer_secret +“&”)对OAuthGetRequestToken签名base_string,并使用(consumer_secret +“&”+ token_secret)对OAuthGetAccessToken签名base_string

http://hueniverse.com/2008/10/beginners-guide-to-oauth-part-iii-security-architecture/在PLAINTEXT和HMAC-SHA1方法中,共享密钥是Consumer Secret和Token Secret的组合。

龙卷风有谷歌oauth工作代码。 看看这里。 谷歌身份validation 。 我已经使用它,并开箱即用。 所有你需要做的就是摘掉课堂,仔细地把它放到django视图。

PS:龙卷风使用asynchronous模块为用户返回。 由于您使用的是django,因此您需要依靠一些获取variables来确定用户刚刚授予了您的应用程序的访问权限。

IIRC Google oauth并不完全遵循标准,您必须在请求中指定您请求的服务(请参阅google文档中提供的示例)作为附加参数,否则将无法工作。