Drive SDK未列出我的所有文件 [英] Drive SDK not listing all my files

查看:105
本文介绍了Drive SDK未列出我的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图列出驱动器中的所有文件(大约10个),但以下仅列出1个(并且甚至不是我的真实文件)....



代码:

  from httplib2导入Http $ b $ 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']
为文件中的f:
print f ['id'],f ['title']

结果:

 < file_id>如何开始使用云端硬盘

编辑:
这个问题是相似的,但答案是有正确的oauth范围,我已经在上面。

编辑#2:
我认为这可能是一个时间问题,所以我给了它几个小时,仍然没有鹅。



编辑#3:
如果我尝试复制另一个用户的文件,然后列出我的文件,那么我会得到2个文件:
如何开始使用云端硬盘
我的新文件
所以,这只是列出了由该应用创建的文件?我如何获得剩余的文件?

解决方案

您使用服务帐户进行身份验证。服务帐户默认不具有访问您的云端硬盘数据的权限,但仅拥有自己拥有的文件。



您有三个选项可以解决此问题:


  1. 在云端硬盘帐户中创建一个文件夹,并与服务帐户共享(读/写)。您放置在该文件夹中的任何文件都可以通过您和您的服务帐户进行读写。

  2. 如果您使用Google Apps for Business,请设置域名范围代表,以允许您的服务帐户模拟您域中的所有用户。这样您就可以让您的服务帐户像您的实际Google Apps帐户一样运行。
  3. 无论您使用或不使用Google Apps for Business :请勿使用服务帐户,而应使用三方OAuth 。使用三段式OAuth,您将能够生成访问令牌和刷新令牌,以便您的应用程序代表您的实际Google帐户在云端硬盘中运行。请注意,最后一个选项根本不使用服务帐户。


最简单的显然是选项(1)。如果这是不可接受的,那么我会选择(3),除非你真的想模仿你域中的所有用户。


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???

解决方案

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.

这篇关于Drive SDK未列出我的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆