2012-04-22 13 views
15

मैं उत्तर के लिए 2 दिन खोज रहा हूं, लेकिन कुछ भी नहीं आया।पायथन ओथ 2 - Google के साथ लॉगिन

मैं Django पर Google के साथ लॉगिन के लिए एकीकृत Oauth2 बनाने की कोशिश कर रहा हूं। कोड मैंने अपवाद फेंक दिया है - "टोकन अमान्य है"।

यह तब होता है:

resp, content = client.request(access_token_url, "POST") 
    if resp['status'] != '200': 
     print content 
     raise Exception("Invalid response from Google."+content) 
google_authenticate में

()

कृपया, मेरी मदद करो।

मेरे कोड:

def google_login(request): 
    scope = "https://www.googleapis.com/auth/userinfo.profile" 

    request_token_url = "https://www.google.com/accounts/OAuthGetRequestToken?scope=%s" % scope 
    authorize_url = 'https://www.google.com/accounts/OAuthAuthorizeToken' 
    authenticate_url = "https://accounts.google.com/o/oauth2/auth" 

    response_type = "code" 
    redirect_uri = "http://127.0.0.1:8000/login/google/auth" 
    scope = "https://www.googleapis.com/auth/userinfo.profile" 

    oauth_key = settings.GOOGLE_KEY 
    oauth_secret = settings.GOOGLE_SECRET 

    consumer = oauth.Consumer(oauth_key, oauth_secret) 
    client = oauth.Client(consumer) 

    # Step 1: Get a request token. This is a temporary token that is used for 
    # having the user authorize an access token and to sign the request to obtain 
    # said access token. 

    resp, content = client.request(request_token_url, "POST") 
    request_token = dict(urlparse.parse_qsl(content)) 

    if resp['status'] != '200': 
     raise Exception("Invalid response from Google.") 

    # Step 2. Store the request token in a session for later use. 
    request.session['request_token'] = dict(cgi.parse_qsl(content)) 

    # Step 3. Redirect the user to the authentication URL. 
    url = "%s?oauth_token=%s&client_id=%s&response_type=%s&redirect_uri=%s&scope=%s" % (authenticate_url, 
     request.session['request_token']['oauth_token'], 
     oauth_key,response_type,redirect_uri,scope) 

    return HttpResponseRedirect(url) 

def google_authenticate(request): 
    access_token_url = 'https://www.google.com/accounts/OAuthGetAccessToken' 

    oauth_key = settings.GOOGLE_KEY 
    oauth_secret = settings.GOOGLE_SECRET 

    consumer = oauth.Consumer(oauth_key, oauth_secret) 

    # Step 1. Use the request token in the session to build a new client. 
    token = oauth.Token(request.session['request_token']['oauth_token'], 
     request.session['request_token']['oauth_token_secret']) 
    if 'oauth_verifier' in request.GET: 
     token.set_verifier(request.GET['oauth_verifier']) 
    client = oauth.Client(consumer, token) 

    # Step 2. Request the authorized access token from Google. 
    resp, content = client.request(access_token_url, "POST") 
    if resp['status'] != '200': 
     print content 
     raise Exception("Invalid response from Google."+content) 

    access_token = dict(cgi.parse_qsl(content)) 

    # Step 3. Lookup the user or create them if they don't exist. 
    try: 
     user = User.objects.get(username=access_token['screen_name']) 
    except User.DoesNotExist: 
     # When creating the user I just use their [email protected] 
     # for their email and the oauth_token_secret for their password. 
     # These two things will likely never be used. Alternatively, you 
     # can prompt them for their email here. Either way, the password 
     # should never be used. 
     user = User.objects.create_user(access_token['screen_name'], 
      '%[email protected]' % access_token['screen_name'], 
      access_token['oauth_token_secret']) 

     # Save our permanent token and secret for later. 
     profile = Profile() 
     profile.user = user 
     profile.oauth_token = access_token['oauth_token'] 
     profile.oauth_secret = access_token['oauth_token_secret'] 
     profile.save() 

    # Authenticate the user and log them in using Django's pre-built 
    # functions for these things. 
    user = authenticate(username=access_token['screen_name'], 
     password=access_token['oauth_token_secret']) 
    login(request, user) 

    return HttpResponseRedirect('/') 

उत्तर

23

एक लंबे समय के बाद, और कई घंटे बर्बाद करने के लिए चला गया है बिताया है, मैं OAuth2 के साथ छोड़ दिया, क्योंकि यह कॉन्फ़िगर करने के लिए मुश्किल है, और सब मैं की जरूरत है एक उपयोगकर्ता लॉग इन करने की है। निम्नलिखित कोड को किसी ऐसे व्यक्ति की मदद करनी चाहिए जो कुछ ऐसा करने की ज़रूरत है, और इसे अनुकूलित किया जा सकता है। मैंने जो कुछ किया वह यूआरएल बनाने के निर्देशों का पालन करता था और यहां ->https://developers.google.com/accounts/docs/OAuth2Login

मैंने दो विचार किए (किसी के लिए Django - पृष्ठों का उपयोग नहीं किया) और पहले के लिए एक लिंक बनाया: इस पृष्ठ को मैंने लॉगिन/गूगल और लॉगिन पेज से इसका एक लिंक बनाया।

def google_login(request): 
    token_request_uri = "https://accounts.google.com/o/oauth2/auth" 
    response_type = "code" 
    client_id = XXXXXX-your_client_id 
    redirect_uri = "http://mysite/login/google/auth" 
    scope = "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email" 
    url = "{token_request_uri}?response_type={response_type}&client_id={client_id}&redirect_uri={redirect_uri}&scope={scope}".format(
     token_request_uri = token_request_uri, 
     response_type = response_type, 
     client_id = client_id, 
     redirect_uri = redirect_uri, 
     scope = scope) 
    return HttpResponseRedirect(url) 

ऊपर कोड दूसरा पृष्ठ पर पुनः निर्देशित (इस पृष्ठ गूगल एप्लिकेशन परिभाषा में एक रीडायरेक्ट uri के रूप में परिभाषित किया जाना चाहिए)।

def google_authenticate(request): 
    parser = Http() 
    login_failed_url = '/' 
    if 'error' in request.GET or 'code' not in request.GET: 
     return HttpResponseRedirect('{loginfailed}'.format(loginfailed = login_failed_url)) 

    access_token_uri = 'https://accounts.google.com/o/oauth2/token' 
    redirect_uri = "http://mysite/login/google/auth" 
    params = urllib.urlencode({ 
     'code':request.GET['code'], 
     'redirect_uri':redirect_uri, 
     'client_id':XXXXX_your_google_key, 
     'client_secret':XXXXX_your_google_secret, 
     'grant_type':'authorization_code' 
    }) 
    headers={'content-type':'application/x-www-form-urlencoded'} 
    resp, content = parser.request(access_token_uri, method = 'POST', body = params, headers = headers) 
    token_data = jsonDecode(content) 
    resp, content = parser.request("https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken}".format(accessToken=token_data['access_token'])) 
    #this gets the google profile!! 
    google_profile = jsonDecode(content) 
    #log the user in--> 
    #HERE YOU LOG THE USER IN, OR ANYTHING ELSE YOU WANT 
    #THEN REDIRECT TO PROTECTED PAGE 
    return HttpResponseRedirect('/dashboard') 

मैं वास्तव में इस वहाँ बाहर लोगों की मदद करता है, और उन्हें घंटे मैं बर्बाद बचाता है आशा: मैं इस पृष्ठ लॉगिन/गूगल/प्रमाणन कहा जाता है। कोड पर टिप्पणियां स्वागत से अधिक हैं!

+7

यदि कोई सोच रहा है, तो अब Google से oauth2client लाइब्रेरी है जो क्रेडेंशियल्स के लिए प्रवाह और संग्रहण लागू करती है => अब सभी https://developers.google.com/api-client-library/python/ में सभी 10 लाइनें लेती हैं गाइड/aaa_oauth http://code.google.com/p/google-api-python-client/source/browse/#hg%2Fsamples%2Fdjango_sample – lajarre

+1

@ user1160475 developers.google.com पर एक नज़र डालें। आपके पास पहले से ही एक Google उपयोगकर्ता नाम और पासवर्ड होना चाहिए, लॉग इन होना चाहिए। – Meir

+0

धन्यवाद। कोड काम करता है। मुझे यह जानने के लिए कुछ समय लगा कि एचटीपी() वर्ग को httplib2 में परिभाषित किया गया है। –

संबंधित मुद्दे