Google API Drive V3 retrieving drive storage space used

*爱你&永不变心* 提交于 2020-02-08 10:05:05

问题


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.

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.


回答1:


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.




回答2:


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:

{'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
  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
  6. Come back to Service accounts, select your account, Enable G Suite Domain-wide Delegation
  7. Create Service Account KEY (download it as .json)
  8. Activate Drive API for your project. Go to APIs & Services > Dashboard, click on ENABLE APIS AND SERVICES, search for Drive and Enable it.
  9. Create index.py file with the next code and launch it:
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:

  • Google Drive API V3 - About - get
  • Understanding service accounts


来源:https://stackoverflow.com/questions/59648637/google-api-drive-v3-retrieving-drive-storage-space-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!