2015-04-24 17 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示例客戶端代碼。