Drive SDK not listing all my files

痞子三分冷 提交于 2019-12-06 14:48:26

问题


I am trying to list all the files in my drive (about 10) but the following will only list 1 (and that isn't even a real file of mine)....

the code:

from httplib2 import Http
from oauth2client.client import SignedJwtAssertionCredentials

client='my_client_id'
client_email = 'my_client_email'
with open("/path/to/file.p12") as f:
    private_key = f.read()

credentials = SignedJwtAssertionCredentials(client_email, private_key, 'https://www.googleapis.com/auth/drive')
http_auth = credentials.authorize(Http())
drive_service = build('drive', 'v2', http=http_auth)
r = drive_service.files().list().execute()
files = r['items']
for f in files:
    print f['id'], f['title']

result:

"<file_id> How to get started with Drive"

EDIT: This question is similar but the answer is to have the correct oauth scope, which I have above.

EDIT #2: I thought it might be a timing issue so I gave it a few hours and still no goose.

EDIT #3: If I try to copy a file from another user then list my files then I'll get 2 files: " How to get started with Drive" " My New File" So, this is just listing files created by that app? How do I get the rest of my files???


回答1:


You use a service account to authenticate. A service account does not have by default the right to access your Drive data, but only files that it owns by itself.

You have three options to work around this :

  1. Create a folder in your Drive account, and share it (read/write) with the service account. Any file you place in that folder will be readable and writable both by you and your service account.

  2. If you use Google Apps For Business, setup domain wide delegation to allow your service account to impersonate all users in your domain. That way you will be able to get your service account to behave as if it were your actual Google Apps account.

  3. Whether you use or not Google Apps For Business : do not use a service account but rather 3-legged OAuth. With 3-legged OAuth you will be able to generate an access token and a refresh token that will allow your application to act in Drive on behalf of your actual Google account. Note that this last options does not use service accounts at all.

The simplest is obviously option (1). If it is not acceptable then I would go with option (3), unless you actually want to be able to impersonate all the users in your domain.



来源:https://stackoverflow.com/questions/27759077/drive-sdk-not-listing-all-my-files

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