2015-04-24 18 views
0

我已成功在Google App Engine上创建了一个与Gmail API交互的python web-app。我现在想得到的是,当给出权限时,我的应用程序还会创建一个用户帐户,用户可以使用他们的Google帐户登录。这将阻止用户不得不为我的应用输入另一个密码。向GMail API授权添加单点登录

总之,目前的工作流程是: 1.用户通过电子邮件和密码输入创建帐户 2.用户授权Gmail的存取

我想获得的是:1。 用户连接谷歌帐户和随即获得一个帐户并验证GMail访问

这样做的最佳方法是什么?

我目前的google API代码如下所示。直接从谷歌这里的例子:https://developers.google.com/api-client-library/python/guide/google_app_engine

from apiclient.discovery import build 
from google.appengine.ext import webapp 
from oauth2client.appengine import OAuth2Decorator 

decorator = OAuth2Decorator(
    client_id='your_client_id', 
    client_secret='your_client_secret', 
    scope='https://www.googleapis.com/auth/calendar') 

service = build('calendar', 'v3') 

class MainHandler(webapp.RequestHandler): 

    @decorator.oauth_required 
    def get(self): 
    # Get the authorized Http object created by the decorator. 
    http = decorator.http() 
    # Call the service using the authorized Http object. 
    request = service.events().list(calendarId='primary') 
    response = request.execute(http=http) 

回答

1
  1. 首先访问谷歌的API,你必须在开发者控制台注册。检查这link

  2. 我知道你的流程是,用户登录与gmail id和密码和用户必须提供访问您的应用程序。在使用oauth流程时,如果您在代码中选择'access_type'='offline',即使用户处于脱机状态,也可以帮助访问信息。为此,您需要refresh token

  3. 在您的开发人员控制台中,您必须激活应用程序要使用的所有API。

  4. 在上面提到的Oauth链接中,您可以检查如何处理授权请求以及如何验证用户。

  5. 如果你想要去service account,那么不要忘记做domain wide delegation

  6. 在决定scopes时,如果您的应用程序没有向用户帐户写入任何信息,请使用只读范围。

请检查此link以了解Python中的Gmail API示例客户端代码。