2011-06-10 17 views
5

Oauth ve openid'i ayrı ayrı uygulamıştım (yani OpenId ile oturum açın, OAuth ile Google Veri API'sına ayrı yetkilendirme) ve birleştirmek istersiniz onlar.Google App Engine'de python ile openid + oauth hibrit için iyi bir örnek/şablon arıyorsunuz

Şu anda

- url: /_ah/login_required 
    script: main.py 

- url: .* 
    script: main.py 
    login: required 

Sonra app.yaml aşağıdaki adres, main.py I var: (netlik için kaldırılmıştır ithalatı) main.py da

def getClient(): 
    client = gdata.calendar.service.CalendarService() 
    consumer_key = 'my-app.appspot.com' 
    consumer_secret = 'consumersecret' 
    client.SetOAuthInputParameters(
     gdata.auth.OAuthSignatureMethod.HMAC_SHA1, 
     consumer_key=consumer_key, 
     consumer_secret=consumer_secret) 
    gdata.alt.appengine.run_on_appengine(client) 
    return client 

class OAuthOne(webapp.RequestHandler): 
    def get(self): 
     client = getClient() 
     request_token = client.FetchOAuthRequestToken(oauth_callback='http://my-app.appspot.com/oauth2') 
     client.SetOAuthToken(request_token) 
     auth_url = client.GenerateOAuthAuthorizationURL() 
     self.redirect(auth_url) 

class OAuthTwo(webapp.RequestHandler): 
    def get(self): 
     client = getClient() 
     token_from_url = gdata.auth.OAuthTokenFromUrl(self.request.uri) 
     if not token_from_url: 
      self.redirect('/oauth') 
     else: 
      client.SetOAuthToken(token_from_url) 
      oauth_verifier = self.request.get('oauth_verifier', default_value='') 
      client.UpgradeToOAuthAccessToken(oauth_verifier=oauth_verifier) 
      self.redirect('/') 

class MainPage(webapp.RequestHandler): 

    def get(self): 
     self.user = users.get_current_user() 
     self.template_values = {} 
     if self.user: 
      # do calendar api stuff here 
      self.template_file = 'templates/index.html' 
     else: 
      self.template_file = 'templates/denied.html' 

     path = os.path.join(os.path.dirname(__file__), self.template_file) 
     self.response.out.write(template.render(path, self.template_values)) 

application = webapp.WSGIApplication(
           [('/oauth', OAuthOne), 
            ('/oauth2', OAuthTwo), 
            ('/_ah/login_required', OpenIDHandler), 
            ('/', MainPage)], 
           debug=True) 

def main(): 
    run_wsgi_app(application) 

if __name__ == "__main__": 
    main() 

, http://code.google.com/googleapps/marketplace/tutorial_python_gae.html hibrid protokolü gelince

class OpenIDHandler(webapp.RequestHandler): 
    def get(self): 
     """Begins the OpenID flow and begins Google Apps discovery for the supplied domain.""" 
     login_url = users.create_login_url(dest_url='http://my-app.appspot.com/', 
              _auth_domain=None, 
              federated_identity='gmail.com') 
     self.redirect(login_url) 

itibaren, bir PHP var örnek here ve bir java örneği here ancak python için hiçbir şey bulamıyorum.

Büyümenin başlamasının OpenIDHandler'ımda olması gerektiğini ve users.create_login_url()'dan başka bir şey kullanmam gerektiğini varsayalım. Google'ın dokümanları here, 'Keşif gerçekleştirme ve kimlik doğrulama talepleri yapma mekanizmasını oluşturma' gerektiğimi söylüyor. ve 'Kimlik doğrulama isteklerine OAuth özelliği ekle' (daha fazla belge here), ancak bunu nasıl yapacağımı bilmediğim kadarıyla. En azından Python ile değil.

this page

https://www.google.com/accounts/o8/id 
?openid.ns=http://specs.openid.net/auth/2.0 
&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select 
&openid.identity=http://specs.openid.net/auth/2.0/identifier_select 
&openid.return_to=http://www.example.com/checkauth 
&openid.realm=http://www.example.com 
&openid.assoc_handle=ABSmpf6DNMw 
&openid.mode=checkid_setup 
&openid.ns.oauth=http://specs.openid.net/extensions/oauth/1.0 
&openid.oauth.consumer=www.example.com 
&openid.oauth.scope=http://docs.google.com/feeds/+http://spreadsheets.google.com/feeds/ 

üzerinde ham http isteği biraz daha düşük bir örnektir vardır Ama bu nasıl kullanılacağını emin değilim.

Bu, en iyi uygulamanın parlak bir örneği haline gelmesine yardımcı olmanın yanı sıra, 'Kimlik doğrulama isteklerine OAuth özelliği ekleme' konusunu gerçekten bilmem gerek.

+0

Aynı problem var, python'da hibrit protokol kullanmak istiyorum ama hiçbir örnek bulamadım. Bununla başardın mı? Eğer evet ise, lütfen kod örneği gönderin. –

cevap