【问题标题】:PYTHON Connect to Google Team DrivePYTHON 连接到 Google 团队云端硬盘
【发布时间】:2018-08-17 06:31:47
【问题描述】:

我正在尝试使用服务帐户访问 google team drive

1)我去了团队驱动器并添加为会员服务帐户电子邮件,并授予他完全访问权限

2) 我不确定必须(在控制台内)为此服务帐户授予哪个权限才能连接到 google team drive

这是我正在使用的代码

def initialize_analyticsreporting():

print("Wait for Initialising")
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    "key.json", "https://www.googleapis.com/auth/drive")

# Build the service object.
analytics = build('drive', 'v3', credentials=credentials)
print(analytics)
print("Service for Google Drive - Initialised")

return analytics


def main(analytics):

    page_token = '1'
    while True:
        response = analytics.files().list(q="mimeType='image/jpeg'",
                                            spaces='drive',
                                            fields='nextPageToken, files(id, name)',
                                            pageToken=page_token).execute()
        for file in response.get('files', []):
            # Process change
            print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            print("Token not initilied")
            break
    return response

if __name__ == '__main__':
    analytics = initialize_analyticsreporting()
    main(analytics)

身份验证部分工作正常。

它失败了,因为我不确定 page_token 是什么。

我需要什么:我需要使用此服务帐户连接到 Team Drive,并获取每个类别的文件数量,例如有多少视频、图片等(如果可能)

【问题讨论】:

    标签: python google-drive-api google-drive-realtime-api service-accounts google-drive-shared-drive


    【解决方案1】:

    正如您在the documentation 中看到的,pageToken 是在下一页继续上一个请求的令牌。

    假设您在团队云端硬盘中有 1000 个文档,并且您使用 100 个 pageSize,在第一个请求中您应该发送一个空的 pageToken,响应将是 100 个文件和一个 nextPageToken。如果您使用该 pageToken 执行另一个 files.list 请求,您将从该团队驱动器中获取接下来的 100 个文件,依此类推。

    因此,在您的情况下,您需要发送一个空的 pageToken 而不是“1”。

    希望清楚!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 2019-02-28
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多