2011-10-18 17 views
10

मैं प्रमाणीकरण के लिए oauth 2.0 प्रदाता के साथ एक अतिरिक्त एपीआई सेट अप करना चाहता हूं। मैं अजगर का उपयोग करता हूं। क्या ऐप इंजन पर चलने वाले पायथन में कोड किए गए ओथ 2.0 प्रदाता को स्थापित करने के लिए कोई लाइब्रेरी है? धन्यवाद।google app engine oauth2 प्रदाता

उत्तर

2

क्या आपने OAuth for Python लेख जानकारी की जांच की है? यह कहता है कि यह "संदर्भ में वर्णन करता है कि ओथ को पाइथन अनुप्रयोगों के साथ सेवा प्रदाता के रूप में कैसे उपयोग करें।"

+0

प्रमाणीकरण नहीं मेरी सेवा है जो मेरी प्रमाणीकरण बनाता है पूरी तरह से गूगल पर निर्भर के लिए गूगल का उपयोग करता है! – neo

+0

फिर पाइथन में एक पूर्ण OAuth कार्यान्वयन के लिए [python-oauth2] (https://github.com/simplegeo/python-oauth2) देखें। –

+1

मुझे लगता है कि नाम के बावजूद इसका उपयोग केवल ओथ 1.0 प्रदाताओं – neo

22

OAuth2 पाइथन और जावा ऐप इंजन रनटाइम दोनों पर अंतर्निहित समर्थन।

अजगर में सभी की जरूरत है:

from google.appengine.api import oauth 

# Note, unlike in the Android app below, there's no 'oauth2:' prefix here 
SCOPE = 'https://www.googleapis.com/auth/userinfo.email' 

# magic happens here 
user = oauth.get_current_user(SCOPE) 

जावा में आप का प्रयोग करेंगे:

from google.appengine.api import oauth 
import logging 
import traceback 
import webapp2 


class MainHandler(webapp2.RequestHandler): 

    def post(self): 
    self.response.headers['Content-Type'] = 'text/plain' 
    self.response.write('Hi there!\n') 

    # Note, unlike in the Android app below, there's no 'oauth2:' prefix here 
    scope = 'https://www.googleapis.com/auth/userinfo.email' 
    try: 
     self.response.write('\noauth.get_current_user(%s)' % repr(scope)) 

     # validates audience of the OAuth2 access token 
     allowed_clients = ['407408718192.apps.googleusercontent.com'] # list your client ids here 
     token_audience = oauth.get_client_id(scope) 
     if token_audience not in allowed_clients: 
     raise oauth.OAuthRequestError('audience of token \'%s\' is not in allowed list (%s)' % (token_audience, allowed_clients))   

     # gets user object for the user represented by the oauth token 
     user = oauth.get_current_user(scope) 
     self.response.write(' = %s\n' % user) 
     self.response.write('- auth_domain = %s\n' % user.auth_domain()) 
     self.response.write('- email  = %s\n' % user.email()) 
     self.response.write('- nickname = %s\n' % user.nickname()) 
     self.response.write('- user_id  = %s\n' % user.user_id()) 
    except oauth.OAuthRequestError, e: 
     self.response.set_status(401) 
     self.response.write(' -> %s %s\n' % (e.__class__.__name__, e.message)) 
     logging.warn(traceback.format_exc()) 


app = webapp2.WSGIApplication([ 
    ('/.*', MainHandler) 
], debug=True) 
:

OAuthService oauth = OAuthServiceFactory.getOAuthService(); 

// Note, unlike in the Android app below, there's no 'oauth2:' prefix here 
String SCOPE = "https://www.googleapis.com/auth/userinfo.email"; 

// magic happens here 
User user = oauth.getCurrentUser(SCOPE); 

यहाँ पूर्ण अजगर 2.7 हैंडलर जो आप उपयोगकर्ता की पुष्टि कर सकेगा है

app.yaml छोटा है

application: your-app-id 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: true 

handlers: 
- url: /favicon\.ico 
    static_files: favicon.ico 
    upload: favicon\.ico 

- url: .* 
    script: main.app 

ध्यान दें कि ग्राहक को Authorization: Bearer HTTP अनुरोध शीर्षलेख में OAuth2 टोकन भेजना चाहिए, उदा।

AccountManager accountManager = AccountManager.get(this); 
Account[] accounts = accountManager.getAccountsByType("com.google"); 

// TODO: Allow the user to specify which account to authenticate with 
for (Account account : accounts) { 
    Log.i(TAG, "- account.name = " + account.name); 
} 

// Note the "oauth2:" prefix here 
String authTokenType = "oauth2:https://www.googleapis.com/auth/userinfo.email"; 

// Note: AccountManager will cache these token, even after they've expired. 
// TODO: Invalidate expired tokens, either after auth fails, or preemptively via: 
// accountManager.invalidateAuthToken(accounts[0].type, token); 

accountManager.getAuthToken(accounts[0], authTokenType, null, this, 
    new AccountManagerCallback<Bundle>() { 
     @Override 
     public void run(AccountManagerFuture<Bundle> future) { 
     try { 
      String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN); 
      Log.i(TAG, "Got KEY_AUTHTOKEN: " + token); 
      // Don't forget HTTP Header "Authorization: Bearer <token>" 
      callAppEngineRestApi(token); // <---- Your code here 
     } catch (OperationCanceledException e) { 
      Log.i(TAG, "The user has denied you access to the API"); 
     } catch (Exception e) { 
      Log.i(TAG, "Exception: ", e); 
     } 
     } 
    }, null); 

आप एक साथ रखा सब कुछ देखने के लिए चाहते हैं, तो चेकआउट के लिए स्वतंत्र महसूस:

Authorization: Bearer ya29XAHES6ZT4w72FecXjZu4ZWskTSX3x3OqYxUSTIrA2IfxDDPpI 

आपका Android ऐप्लिकेशन का निर्माण कर रहे हैं तो, आप आसानी से इन टोकन AccountManager इंटरफ़ेस का उपयोग कर उत्पन्न कर सकते हैं

+0

बनाने के लिए किया जा सकता है? अफैइक जीए सिर्फ ओथ 1.0a – systempuntoout

+0

का समर्थन करता है, मुझे आपके कोड का परीक्षण किया गया था लेकिन यह काम नहीं करता है - अपवाद: - मैं अजगर 2.5 का उपयोग किया गया था। – Chameleon

+0

Google और StackOverflow के आस-पास बहुत कुछ देखा। मुझे मिला अच्छा काम कोड के साथ यह एकमात्र जवाब है। (एक पूर्ण उदाहरण के लिए आपूर्ति यूआरएल देखें)। जावा के लिए भी काम किया, इसलिए प्रश्न शीर्षक जावा उपयोगकर्ताओं को गुमराह कर सकता है। – Guy

0

मैं ऊपर जवाब पर टिप्पणी नहीं कर सकता तो मैं इसे यहाँ इस स्निपेट के साथ संघर्ष कर किसी के लिए भी जोड़ लिया है:

# magic happens here 
user = oauth.get_current_user(SCOPE) 

यह यदि आप सेवा खातों का उपयोग कर रहे हैं (और आज के रूप में, मुझे लगता है कि Google उपयोगकर्ता टोकन भी) तो एक महीने के लिए ऐपइंजिन पर तोड़ दिया गया है क्योंकि टोकन की लंबाई एई लाइब्रेरी में समस्याएं पैदा करती है। Google ने मुझे बताया है कि जल्द ही इसे ठीक करने की संभावना नहीं है।

यह केवल बात यह है कि मेरे लिए काम करता है, इस समय है:

token = self.request.headers['Authorization'].split(' ')[1] 
    url = 'https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=' + token 
    oauth_response = urlfetch.fetch(url) 
    if oauth_response.status_code != 200: 
     raise Exception('Unable to authorise: {}/{}'.format(oauth_response.status_code, oauth_response.content)) 
    token_response = json.loads(oauth_response.content) 
    email = token_response['email'] 
+0

यह क्या करता है? इसके पीछे तर्क क्या है? – Praxiteles

+0

यह प्रतिक्रिया से ईमेल पते निकालने, भालू टोकन को 'डीकोड' करने के लिए Google rest api का उपयोग करता है। – KevH