Google Drive API无法从我的驱动器列出文件(Ruby)

时间:2015-03-13 16:57:03

标签: ruby google-api google-drive-api

我正在开发一个Ruby程序来访问我的google驱动器以获取我的文件。 我想使用服务器到服务器连接来避免人们接受权限,因此我使用为服务帐户客户端ID 生成的p12密钥。 连接正常,但我成功插入了一个文件。

我的问题是当我尝试列出我的所有文件时:调用效果很好,但我只得到2个文件:先前插入的文档,以及一个名为“如何开始使用Drive”的pdf (我的很多其他文件,如文档,电子表格,演示文稿,图片等......)。

我用我的谷歌帐户创建了我的密钥,但我无法访问我的文件。此外,我使用我的程序插入的文档没有出现在我的文件中的谷歌驱动器中。

我错过了关于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 => [])

非常感谢!

儒略

1 个答案:

答案 0 :(得分:2)

啊,这就是问题所在。服务帐户不是您通过浏览器更新的云端硬盘帐户。请参阅Google Drive help required access to own Drive accountHow do I authorise an app (web or installed) without user intervention? (canonical ?)。顺便说一句,在你的代码中,files.list上面的评论说"上传文件"这就是我第一次错过它的原因: - )

相关问题