问题
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:
- Create project in Admin Console
- Create service account
- Go to Admin Console > Security > Advanced settings > Manage API client access
- In Client Name put the full email of your created Service Account
- In One or More API Scopes put
https://www.googleapis.com/auth/drive
and click Authorize - Come back to Service accounts, select your account, Enable G Suite Domain-wide Delegation
- Create Service Account KEY (download it as .json)
- Activate Drive API for your project. Go to APIs & Services > Dashboard, click on ENABLE APIS AND SERVICES, search for Drive and Enable it.
- 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