如何存储凭据Gmail API

时间:2018-05-14 13:08:01

标签: python django web gmail-api

我已将Gmail API与Django项目相关联。我在存储每个用户的凭据时遇到问题。存储它们的最正确方法是什么,因此每个用户都可以登录?没有存储它只是为一个用户创建一个文件,这就是全部。我应该创建一个数据库吗?

这是Google的“快速入门”文件:

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from django.contrib.auth.models import User


# user = User
# storage = (CredentialsModel, 'id', user, 'credential') As you see I tried it here but it didn't go well
# credential = storage
#
# storage.__add__(credential)

# Setup the Gmail API
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
        creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))


def go():

    # Call the Gmail API
    results = service.users().labels().list(userId='me').execute()
    labels = results.get('labels', [])

    # if not labels:
    #     print('No labels found.')
    # else:
    #     print('Labels:')
    #     for label in labels:
    #         print(label['name'])

pass

0 个答案:

没有答案