简体   繁体   English

我如何在Google Appengine应用程序的数据库中验证用户(任何人都可以推荐对Google Appengine应用程序进行用户身份验证的最佳方法)?

[英]How do i verify user in database in google appengine app ( can anyone recommend the best way to do user authentication for google appengine app)?

I have following code in models.py i can sort database by only key but not by string ? 我在models.py有以下代码,我只能按键而不是按字符串对数据库进行排序?

from google.appengine.ext import ndb

class Roles(ndb.Model):
name = ndb.StringProperty()
owner = ndb.KeyProperty(kind='User')
created = ndb.DateTimeProperty(required=True, auto_now_add = True)
class RESTMeta:
    user_owner_property = 'owner'
    include_output_properties = ['name']

class Users(ndb.Model):
name = ndb.StringProperty()
email = ndb.StringProperty()
password = ndb.StringProperty()
roles = ndb.KeyProperty(kind='Roles')
owner = ndb.KeyProperty(kind='User')
created = ndb.DateTimeProperty(required=True, auto_now_add = True)
class RESTMeta:
    user_owner_property = 'owner'
    include_output_properties = ['name']

And the following in api.py 以及api.py的以下api.py

app = webapp2.WSGIApplication([
RESTHandler(
  '/api/roles', # The base URL for this model's endpoints
  models.Roles, # The model to wrap
  permissions={
    'GET': PERMISSION_ANYONE,
    'POST': PERMISSION_ANYONE,
    'PUT': PERMISSION_OWNER_USER,
    'DELETE': PERMISSION_ADMIN
  },
  # Will be called for every PUT, right before the model is saved (also supports callbacks for GET/POST/DELETE)
  put_callback=lambda model, data: model
),
RESTHandler(
  '/api/users', # The base URL for this model's endpoints
  models.Users, # The model to wrap
  permissions={
    'GET': PERMISSION_ANYONE,
    'POST': PERMISSION_ANYONE,
    'PUT': PERMISSION_OWNER_USER,
    'DELETE': PERMISSION_ADMIN
  },
  # Will be called for every PUT, right before the model is saved (also supports callbacks for GET/POST/DELETE)
  put_callback=lambda model, data: model
)],debug=True, config = config)

I can successfully get by key in api\\users?q=roles=key('key') 我可以通过api\\users?q=roles=key('key')的键成功get api\\users?q=roles=key('key')

How do i get specific user by String api\\users?q=email=String('String') 如何通过String api\\users?q=email=String('String')获取特定用户api\\users?q=email=String('String')

The Question is how do I do user auth for google appengine app 问题是我该如何对Google Appengine应用程序进行用户身份验证

You seem to be asking so many questions in one. 您似乎一口气问了很多问题。

To get user by email, simply do this: 要通过电子邮件获取用户,只需执行以下操作:

users = Users.query(Users.email=='query_email').fetch(1)
#note fetch() always returns a list
if users:
    user_exists = True
else:
    user_exists = False

Please note, you may need to update your datastore index to support that query. 请注意,您可能需要更新数据存储区索引以支持该查询。 The easiest way to do it is to first run the code in your local development server, and the index will be automatically updated for you. 最简单的方法是首先在本地开发服务器中运行代码,索引将自动为您更新。

To answer your second questions, for user authentication I would recommend Django's in-built User Authentication . 为了回答您的第二个问题,对于用户身份验证,我建议使用Django的内置用户身份验证 Please note that you can always run vanilla django on appengine with a Flexible VM using CloudSQL instead of the Datastore. 请注意,您始终可以使用CloudSQL(而不是数据存储区) 在具有弹性VM的appengine上运行vanilla django

Alternatively, you could use the Appengine Users API , though your users would need to have Google Accounts. 另外,您可以使用Appengine用户API ,尽管您的用户需要具有Google帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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