3

我有一个 Python GAE 应用程序,它为用户提供一个 HTML 登录页面。此页面包含一个 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 感谢您的帮助。

更新:我想通了

登录.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

为了处理回调,我必须在我的

应用程序.yaml:

- url: /oauth2callback
  script: oauth2client/appengine.py

- url: .*
  script: login.py
4

0 回答 0