ユーザーに HTML ログインページを表示する Python GAE アプリケーションがあります。このページには、アプリケーションにログインするための oauth2 ログイン リンク (response_type=code) が含まれています。リンクは次のようになります。
https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&state=%2Fprofile&redirect_uri=https%3A%2F%2Foauth2-login-demo.appspot.com%2Foauthcallback&response_type=code&client_id=812741506391.apps.googleusercontent.com
ユーザーがアクセスを許可すると、GAE アプリケーションは次のようなコールバックを受け取ります。
https://oauth2-login-demo.appspot.com/oauthcallback?code={authorizationCode}
私の質問:
Python oauth2 デコレータを使用してこのコールバックを処理できますか = 認証コードをアクセス トークンと交換します。
デコレーターを使用して、HTML ページの oauth2 ログイン リンクを作成することもできますか?
私はこのフローを使用しています: https://developers.google.com/accounts/docs/OAuth2Login。
そしてデコレーター: https://developers.google.com/api-client-library/python/platforms/google_app_engine ご協力ありがとうございます。
更新:私はそれを理解しました。
login.py
from google.appengine.api import users
from oauth2client.appengine import OAuth2DecoratorFromClientSecrets
decorator = OAuth2DecoratorFromClientSecrets(
os.path.join(os.path.dirname(__file__), 'client_secrets.json'), 'https://www.googleapis.com/auth/userinfo.email')
class LoginHandler(BaseHandler):
@decorator.oauth_aware
def get(self):
if decorator has credentials :
http = httplib2.Http() # verify audience using the TokenInfo API
body = urllib.urlencode({'access_token': decorator.credentials.access_token})
resp, content = http.request("https://www.googleapis.com/oauth2/v1/tokeninfo",method="POST", body=body)
.... # render template and show the user info from the TokenInfo response
else:
login_url = decorator.authorize_url()
.... # render template and show the login_url
コールバックを処理するには、2 行追加する必要がありました。
app.yaml:
- url: /oauth2callback
script: oauth2client/appengine.py
- url: .*
script: login.py