首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Google Drive API oauth2 -保存用户的授权同意

Python Google Drive API oauth2 -保存用户的授权同意
EN

Stack Overflow用户
提问于 2018-06-15 04:38:35
回答 1查看 2K关注 0票数 1

我正在创建一个MacOS应用程序,它在给定的Google Team Drive/文件夹中创建一个自定义文件夹层次结构。每次我运行应用程序时,它都会打开一个浏览器,要求用户登录并允许应用程序访问。在接受时,应用程序会创建文件夹并按预期工作。然而,它每次都会这样做,我很难找到如何保存“用户同意”,这样他们应该只允许同意一次,他们第一次运行应用程序。我已经尝试了许多不同的选择,但一无所获,所以我真的很感谢大家的帮助。

代码语言:javascript
复制
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow

SCOPES = ['https://www.googleapis.com/auth/drive']
API_SERVICE_NAME = 'drive'
API_VERSION = 'v3'

def get_authenticated_service():
    client_secrets = args["secrets"] #client_secrets.json file passed as "secrets" argument

    flow = InstalledAppFlow.from_client_secrets_file(client_secrets, SCOPES)

    credentials = flow.run_local_server(host='localhost',
                                    port=8080,
                                    authorization_prompt_message='Please visit this URL: {url}',
                                    success_message='The auth flow is complete; you may close this window.',
                                    open_browser=True)

    return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

if __name__ == '__main__':
    service = get_authenticated_service()
    name = format_client_name()
    create_folders(service, name)

我试过使用Google API Client Libraries here (https://developers.google.com/api-client-library/python/guide/aaa_oauth),它显示了存储,但是它没有任何关于google.oauth的说明,因为你不能在google.oauth中使用oauth2client存储。我觉得这很简单,但我碰壁了。

EN

回答 1

Stack Overflow用户

发布于 2018-06-15 11:12:18

您可以使用oauth2client.file.Storage将凭据存储在credentials.json文件中,以供将来检索。该函数在运行身份验证流之前检查本地存储的有效凭据。

我在下面的repo中附加了一个示例:

https://github.com/gsuitedevs/python-samples/blob/master/drive/quickstart/quickstart.py

代码语言:javascript
复制
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START drive_quickstart]
"""
Shows basic usage of the Drive v3 API.
Creates a Drive v3 API service and prints the names and ids of the last 10 files
the user has access to.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.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('drive', 'v3', http=creds.authorize(Http()))

# Call the Drive v3 API
results = service.files().list(
    pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print('{0} ({1})'.format(item['name'], item['id']))
# [END drive_quickstart]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50865584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档