通過python-social-auth
在Django中添加其他範圍不是開箱即用,但也不難(two ways described here)。我選擇了第一個選項並覆蓋了get_scope
方法。
在views.py
,我成立了
flow = client.flow_from_clientsecrets(
CLIENT_SECRET_FILE,
scope=SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPES,
redirect_uri='https://yourdomain.com/callbacklinkforgmail')
和redirect_uri
應該與谷歌控制檯重定向的URL。
請求權限是根據存儲在某處的憑證完成的。在我的情況下,一個模型。
# model with variable/column 'credentials'
storage = Storage(YourModel, 'id', request.user, 'credentials')
credential = storage.get()
if credential is None or credential.invalid is True:
flow.params['state'] = xsrfutil.generate_token(SOCIAL_AUTH_GOOGLE_OAUTH2_KEY,
request.user)
authorize_url = flow.step1_get_authorize_url()
return HttpResponseRedirect(authorize_url)
else:
http = credential.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
當這一步驟完成後,重定向redirect_uri
到這證實了匹配的功能。在我的情況下,這是在views.py
。
@login_required
def auth_return(request):
if not xsrfutil.validate_token(SOCIAL_AUTH_GOOGLE_OAUTH2_KEY, request.GET['state'].encode('utf-8'), request.user):
return HttpResponseBadRequest()
credential = flow.step2_exchange(request.GET)
storage = Storage(YourModel, 'id', request.user, 'credentials')
storage.put(credential)
return HttpResponseRedirect("/")
,並進入我的應用程序的urls.py
是
url(r'^callbacklinkforgmail/$', views.auth_return, name='mailsend')
客戶端ID訪問您在啓用API部分啓用了所有的API。只要用戶使用範圍電子郵件進行身份驗證(我認爲這是一個或者gmail),您可以使用相同的客戶端ID來訪問許多API。它的認證範圍決定了你可以訪問什麼。上面的消息並沒有告訴我很多。讓我們看看YouTube的視頻唄:) – DaImTo
@DaImTo目前我唯一定義的範圍是[''https://www.googleapis.com/auth/gmail.send''](https://developers.google.com/Gmail的/ API/AUTH /範圍)。我沒有看到(請參閱上一個鏈接),我可以在其中添加「登錄」範圍。希望看到youtube視頻自己。編輯:哼,也許[這](https://developers.google.com/identity/protocols/googlescopes),特別是在谷歌登錄下的'profile'和'login'? –
它已經有一段時間了,但我認爲登錄去通過加https://developers.google.com/+/web/api/rest/oauth btw gmail是一個痛苦的只是說,我不是一個蟒蛇人只是試圖指出你在正確的方向 – DaImTo