我正在研究Ruby程序以访问我的谷歌驱动器以获取我的文件。 我想使用服务器到服务器的连接来避免人们接受权限,所以我使用为服务帐户客户端ID生成的p12密钥。 连接是好的,但我成功地插入了一个文件。Google Drive API没有列出我的驱动器中的文件(Ruby)
我的问题是,当我尝试列出我的所有文件:通话效果很好,但我只得到2个文件:先前插入文档,一个名为PDF“如何开始使用驱动器”(我有一个我的很多其他文件,如文档,电子表格,演示文稿,图片等)。
我用我的Google帐户创建了我的密钥,但无法访问我的文件。另外,我的程序中插入的文档在我的文件中没有出现在我的谷歌驱动器中。
有没有我错过了API?我得到的是什么文件而不是我的? 这真的像我不在同一个Google云端硬盘帐户。 下面是我用我列出文件的代码:
# Prepare the OAuth client/Service Account
client = Google::APIClient.new(application_name: 'Google Drive Ruby test', application_version: '0.0.1')
key = Google::APIClient::KeyUtils.load_from_pkcs12('key.p12', 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/drive',
:issuer => '***@developer.gserviceaccount.com',
:signing_key => key)
client.authorization.fetch_access_token!
# Get the Drive API
drive = client.discovered_api('drive', 'v2')
# Upload a file
api_result = client.execute(
:api_method => drive.files.list,
:parameters => [])
非常感谢您!
Julian
粘贴代码执行files.list和理想的http跟踪。你正确地关注nextPageToken吗?当你说“我的文件”时,你会发现服务帐户不是你的文件,而是一个单独的帐户? – pinoyyid 2015-03-13 18:18:27
这是我正在测试的完整代码。我只是打印api_result.data对象。当我说我的文件时,我想访问我通过浏览器创建的电子表格或文档。 – 2015-03-13 19:01:03
啊,这就是问题所在。服务帐户不是您的云端硬盘帐户,您可以通过浏览器进行更新。请参阅http://stackoverflow.com/questions/28784575/google-drive-help-required-access-to-own-drive-account/28789719#28789719和http://stackoverflow.com/questions/19766912/how-do -i检查-A-背景web应用程序,而无需用户干预规范。顺便说一句,在你的代码上面的评论files.list说“上传文件”,这就是为什么我第一次错过它。 – pinoyyid 2015-03-14 03:02:36