質問に記載されたチュートリアルには特に言及しませんが、ここではgoogle.appengine.apiユーザーモジュールを使用してGoogleユーザーを認証する方法を示します。 JavaScriptを使わずにすべてを行うことができますが、ユーザIDを取得するようなWebサイト特有の機能は、投稿後にJSでチェックするように暗号化されたCookieを設定することで行うことができます(問題があれば、 )。
この回答は、Python2と標準環境で動作するGoogle App Engineアプリケーションに適用されることに注意してください。このアプリもjinja2テンプレートを使用しています。
私は、Googleアカウントを持つユーザーを認証するためにここに利用できるゲストブックアプリを、次の午前:HTML部分については
App Engine Guestbook - authenticating users
:セットアップ
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-client_id" content="MY_CLIENT_ID.apps.googleusercontent.com">
</head>
<body>
<!-- Login Link -->
<!-- <div class="g-signin2" data-onsuccess="onSignIn"></div> -->
<a href="{{ g_url | safe}}">{{ g_url_txt }}</a>
</body>
</html>
ザ・Pythonのバックエンド(main.py) :
import webapp2
# Templating
import jinja2
# Authenticating users with google's users API
from google.appengine.api import users
# GAP's db model (only shown to see how user is checked if a returning user)
from google.appengine.ext import db
# For the templating to work you'll need to set up a
# jinja environment linked to your own template directory, for example:
jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
ハンドラ(m ain.py):
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
template = jinja_env.get_template(template)
return template.render(params) # parameters can also be a dictionary!
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class Login(Handler):
def get(self):
# If user chooses Google Sign-in
user = users.get_current_user() # Returns google user if signed-in
if user: # If google user exists...
nickname = user.nickname()
guser_id = user.user_id() # This is unique, email address may change.
email = user.email()
# Check if google user exists in database:
u = User.by_google_id(guser_id)
if u:
# Do something to give rights to user on your website
# e.g. via encrypted cookie.
else:
u = User(guser_id = guser_id,
email = email,
username = nickname)
u.put()
self.redirect("/welcome")
else: # Otherwise render the google href that will direct the user to the sign-in process.
g_url = users.create_login_url(self.request.uri)
g_url_txt = 'Login with Google'
self.render("login.html", g_url=g_url, g_url_txt=g_url_txt)
# After the user click on the link provided, sign-in will proceed, with succes, the
# redirect back here, with "self.request.uri" and the get() will run again,
# but this time with a google user.
class Welcome(Blog):
def get(self):
g_url = users.create_logout_url("/") # Will redirect to Login page if clicked on.
g_url_txt = 'Logout'
self.render('welcome.html', g_url=g_url, g_url_txt=g_url_txt)
class User(db.Model):
email = db.StringProperty(required = True)
guser_id = db.StringProperty(required = False) # googleUser
nickname = db.StringProperty(required = False) # googleUser
# @decorator:
# means that you can call the
# object's method without instantiating the object
@classmethod
def by_id(cls, uid):
# 'cls' refers to the User class
return cls.get_by_id(uid, parent = users_key())
@classmethod
def by_google_id(cls, gid):
# 'cls' refers to the User class
return cls.all().filter('guser_id =', gid).get()
app = webapp2.WSGIApplication([('/', Login),
('/welcome', Welcome)
],
debug = True)
私は、これは私はまだ利用できませソリューション、また上記のチュートリアルでは、実際に廃止されているかどうかを明確にして、この日も貼り付けた、あなたや他の誰かに役立ちます願っています。
これまでの手順で同様の問題が発生しています。手順は、コンソールのサイドバーから[資格情報]を選択すると表示されますが、そこにはありません。編集:気にしない。サインインは推奨されていません。 –