简体   繁体   中英

How to use Google Acounts API in App Engine?

I have a Python based webapp, in wich one, I would like to use Google Accounts to let the users log in. it is simple, let's say my app is on:

http://myapp.appspot.com/

What I want to do is to "greet" the users with the Google Accounts Auth Screen when they navigate to the app, and only after logged in, let them see the App's interface.

The other way to do it would be adding a static folder, let's say "premium" and in that case, All the users would be able to see app's interface without logging in, but let's say they want to go into:

http://myapp.appspot.com/premium/whatever.htm

So, in that case, I would like to restrict all the "/premium" URL's to the logged in users...

I understand it's easy to do by just adding this handler into my "app.yaml" file:

- url: /premium/.*
  script: premium.py
  login: required

But the thing is, I don't understand what does "premium.py" content has to be...

Can you help me with this confusion? Thanks!

The app.yaml solution should work. But you also asked how to do it in python. Either will work. Both is unnecessary. Here it is:

from google.appengine.api import users

class Premium(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    if not user:
      this.redirect(users.create_login_url(this.request.uri))
    # Your normal page code here

(and I assume you understand the rest, or really you need to read the GAE hello world tutorial)

Clear out your browser cookies! Especially when you are using the local dev environment. You might be logged in and not know it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM