简体   繁体   English

Google API Drive V3 检索使用的驱动器存储空间

[英]Google API Drive V3 retrieving drive storage space used

I'm using a google service account to retrieve the usage of data from different users.我正在使用谷歌服务帐户来检索不同用户的数据使用情况。 I'm using google's python client to authenticate and retrieve the data.我正在使用 google 的 python 客户端来验证和检索数据。

Code代码

    service = build('drive', 'v3', credentials=auth)
    result = service.about().get(email).execute();
    result = result.get("storageQuota", {})

I keep getting the following error:我不断收到以下错误:

    method() takes 1 positional argument but 2 were given

I want to be able to get it from a specific user's drive information using the email as an identifier.我希望能够使用电子邮件作为标识符从特定用户的驱动器信息中获取它。

The is an undocumented required paramater with that request.该请求是一个未记录的必需参数。 Its called fields.它被称为字段。 I have a bug report out.我有一个错误报告。

service = build('drive', 'v3', credentials=auth)
driveRequest = service.about().get(email);
driveRequest.fields = "*";
result = driveRequest.execute();
result = result.get("storageQuota", {})

Kindly note i am not a python developer this is a guess on how to do it.请注意,我不是 Python 开发人员,这是对如何做的猜测。

How to get Drive info from yourself如何从自己那里获取云端硬盘信息

Try this snippet example:试试这个片段示例:

result = service.about().get(fields="*").execute()
result = result.get("storageQuota", {})
print(result)

The print output is: print输出为:

{'usage': '11638750', 'usageInDrive': '11638750', 'usageInDriveTrash': '7531862'}

How to get Drive info from user in your domain如何从您域中的用户获取云端硬盘信息

If you are an admin and want to get users info, do the next steps:如果您是管理员并且想要获取用户信息,请执行以下步骤:

  1. Create project in Admin Console管理控制台中创建项目
  2. Create service account创建服务帐号
  3. Go to Admin Console > Security > Advanced settings > Manage API client access转至管理控制台 > 安全 > 高级设置 > 管理 API 客户端访问
  4. In Client Name put the full email of your created Service Account在客户名称中输入您创建的服务帐户的完整电子邮件
  5. In One or More API Scopes put https://www.googleapis.com/auth/drive and click Authorize在一个或多个 API 范围中输入https://www.googleapis.com/auth/drive并单击授权
  6. Come back to Service accounts , select your account, Enable G Suite Domain-wide Delegation返回服务帐户,选择您的帐户,启用 G Suite 域范围委派
  7. Create Service Account KEY (download it as .json)创建服务帐户密钥(将其下载为 .json)
  8. Activate Drive API for your project.为您的项目激活 Drive API Go to APIs & Services > Dashboard , click on ENABLE APIS AND SERVICES, search for Drive and Enable it.转到 APIs & Services > Dashboard ,单击 ENABLE APIS AND SERVICES,搜索 Drive 并启用它。
  9. Create index.py file with the next code and launch it:使用下一个代码创建index.py文件并启动它:
from googleapiclient.discovery import build
from google.oauth2 import service_account

def main():

    SCOPES = ['https://www.googleapis.com/auth/drive']
    SERVICE_ACCOUNT_FILE = 'serviceaccountsproject-81ec0d3c1c1c.json'

    credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    credentials = credentials.with_subject('user@inYourDomain.com')

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

    result = service.about().get(fields="*").execute()
    result = result.get("storageQuota", {})
    print(result)

if __name__ == '__main__':
    main()

And here is an output:这是一个输出:

{'usage': '0', 'usageInDrive': '0', 'usageInDriveTrash': '0'}


Reference:参考:

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

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