Oauth for Google API example using Python / Django

前端 未结 6 499
傲寒
傲寒 2021-01-30 05:11

I am trying to get Oauth working with the Google API using Python. I have tried different oauth libraries such as oauth, oauth2 and djanog-oauth but I cannot get it to work (inc

6条回答
  •  青春惊慌失措
    2021-01-30 06:14

    This work for me.

    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)
    

提交回复
热议问题