简体   繁体   English

谷歌驱动器 api 和 python

[英]Google drive api with python

I'm trying to make an app where it displays the files and folders inside my google drive folder so I went to google apis and used the code I found on their page its here below我正在尝试制作一个应用程序,它可以在我的 google drive 文件夹中显示文件和文件夹,所以我去了google apis并使用了我在下面的页面上找到的代码

as I didn't expect it just let me log into my drive and then it returned me an error I'm providing a photo for it This is the error returned it says that因为我没想到它只是让我登录到我的驱动器然后它返回了一个错误我正在为它提供一张照片这是返回的错误它说

Message='client_secret' Source=D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py StackTrace: File "D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py", line 48, in main() Message='client_secret' Source=D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py StackTrace: 文件“D:\Study\Computer Science\G10\Paython 2nd semester\ python consol app\python consol app\python_consol_app.py",第 48 行,在 main() 中

I managed to skip this error and renaming the json file and I solved this error only to find another error that says我设法跳过这个错误并重命名 json 文件,我解决了这个错误只是为了找到另一个错误说

Message=Authorized user info was not in the expected format, missing fields client_secret, client_id, refresh_token. Message=授权用户信息不是预期格式,缺少字段 client_secret、client_id、refresh_token。 Source=D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py StackTrace: File "D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py", line 48, in main()源=D:\Study\Computer Science\G10\Paython 第二学期\python consol app\python consol app\python_consol_app.py StackTrace:文件“D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py",第 48 行,在 main() 中

so I'm wondering how should I solve such an issue also if my application can run into every single folder and subfolder所以我想知道如果我的应用程序可以运行到每个文件夹和子文件夹,我应该如何解决这个问题

SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    creds = None
    
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
   
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
      
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('drive', 'v3', credentials=creds)

    
    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(u'{0} ({1})'.format(item['name'], item['id']))

if __name__ == '__main__':
    main()

Answer回答

First things first.第一件事。 It looks like you have some problem with your credentials.您的凭据似乎有问题。 I would download the credentials file from the Quickstart again.我会再次从快速入门下载凭据文件。 Then, delete the generated token file in your working directory.然后,删除工作目录中生成的token文件。

If you want to go deeper, you can create a GCP project, and enable the Google API as well as generate the credentials, but this approach is not necessary if you don't want it.如果您想更深入地了解 go,您可以创建一个 GCP 项目,并启用Google API 并生成凭据,但如果您不想要这种方法,则不需要它。

Once you have Quickstart running, you can focus on your task.运行快速入门后,您可以专注于您的任务。 To display the files within your Drive, there is theFiles: list method, which returns all the files and folders in the current user's My Drive.要显示 Drive 中的文件,有Files: list方法,它返回当前用户的 My Drive 中的所有文件和文件夹。 To perform specific searches, you can use different parameters, but the most important is the query .要执行特定的搜索,您可以使用不同的参数,但最重要的是query You can read this guide for some usage examples and further explanation.您可以阅读本指南以获取一些使用示例和进一步的说明。

References:参考:

first: Enable the API Before using Google APIs, you need to turn them on in a Google Cloud project.第一:启用 API 在使用谷歌APIs之前,你需要在谷歌云项目中开启它们。 You can turn on one or more APIs in a single Google Cloud project.您可以在单个 Google Cloud 项目中启用一个或多个 API。 In the Google Cloud console, enable the Gmail API. from here在谷歌云控制台中,启用 Gmail API。 从这里

secound: Authorize credentials for a desktop application To authenticate as an end user and access user data in your app, you need to create one or more OAuth 2.0 Client IDs.第二:为桌面应用程序授权凭据要以最终用户身份进行身份验证并访问应用程序中的用户数据,您需要创建一个或多个 OAuth 2.0 客户端 ID。 A client ID is used to identify a single app to Google's OAuth servers.客户端 ID 用于识别 Google 的 OAuth 服务器的单个应用程序。 If your app runs on multiple platforms, you must create a separate client ID for each platform.如果您的应用程序在多个平台上运行,您必须为每个平台创建一个单独的客户端 ID。 In the Google Cloud console, go to Menu menu > APIs & Services > Credentials.在 Google Cloud 控制台中,go 转到菜单菜单 > API 和服务 > 凭据。 Go to Credentials Go 到凭据

Click Create Credentials > OAuth client ID.单击创建凭据 > OAuth 客户端 ID。 Click Application type > Desktop app.单击应用程序类型 > 桌面应用程序。 In the Name field, type a name for the credential.在名称字段中,输入凭证的名称。 This name is only shown in the Google Cloud console.此名称仅显示在 Google Cloud 控制台中。 Click Create.单击创建。 The OAuth client created screen appears, showing your new Client ID and Client secret.出现 OAuth 客户端创建屏幕,显示您的新客户端 ID 和客户端密码。 Click OK.单击确定。 The newly created credential appears under OAuth 2.0 Client IDs.新创建的凭据显示在 OAuth 2.0 客户端 ID 下。

last download JSON file and rename file to credentials.json, and move the file to your working directory.最后下载 JSON 文件并将文件重命名为 credentials.json,并将文件移动到您的工作目录。

my problem solved我的问题解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM