Tôi đang cố gắng để Oauth làm việc với API Google bằng cách sử dụng Python. Tôi đã thử các thư viện oauth khác nhau như oauth, oauth2 và djanog-oauth nhưng tôi không thể làm cho nó hoạt động (bao gồm các ví dụ được cung cấp).Oauth cho ví dụ về Google API sử dụng Python/Django
Đối gỡ lỗi Oauth tôi sử dụng Google Oauth Playground và tôi đã nghiên cứu các API và Oauth documentation
Với một số thư viện Tôi đang phải vật lộn với nhận được một chữ ký phải, với các thư viện khác mà tôi đang phải vật lộn với việc chuyển đổi các thẻ yêu cầu một ủy quyền mã thông báo. Điều gì thực sự sẽ giúp tôi nếu ai đó có thể hiển thị cho tôi ví dụ làm việc cho API Google bằng một trong các thư viện nêu trên.
EDIT: Câu hỏi đầu tiên của tôi không dẫn đến bất kỳ câu trả lời nào vì vậy tôi đã thêm mã của mình. Có hai nguyên nhân có thể khiến mã này không hoạt động:
1) Google không ủy quyền mã thông báo yêu cầu của tôi, nhưng không hoàn toàn chắc chắn cách phát hiện này
2) Chữ ký cho mã thông báo truy cập không hợp lệ nhưng sau đó tôi muốn biết thông số oauth mà Google mong đợi khi tôi có thể tạo chữ ký thích hợp trong giai đoạn đầu tiên.
Điều này được viết bằng oauth2.py và cho Django do đó, 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
Cảm ơn !!! Điều này tiết kiệm ngày của tôi !!! – VHanded