Google云端硬盘上的服务帐户需要登录

时间:2013-02-02 18:35:59

标签: ruby-on-rails ruby google-drive-api google-api-ruby-client

我尝试使用Ruby on Rails应用创建/查看Google文档。我认为我能够创建文档,但是当我点击我从API回调中获得的链接时,我收到错误401:需要登录。

这是我用来创建文档的代码。

key = Google::APIClient::PKCS12.load_key(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'notasecret')
asserter = Google::APIClient::JWTAsserter.new(SERVICE_ACCOUNT_EMAIL,
    'https://www.googleapis.com/auth/drive', key)
client = Google::APIClient.new
client.authorization = asserter.authorize()
drive = client.discovered_api('drive', 'v2')
puts 'hello'
file = drive.files.insert.request_schema.new({
  'title' => 'My document',
  'description' => 'A test document',
  'mimeType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
})
puts 'world'
media = Google::APIClient::UploadIO.new(Rails.root.to_s + '/app/assets/documents/document.doc', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
result = client.execute(
  :api_method => drive.files.insert,
  :body_object => file,
  :media => media,
  :parameters => {
  'uploadType' => 'multipart',
  'alt' => 'json'}
)

puts '==========='
puts result
puts '==========='
jj result.data.to_hash

有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您点击的链接是downloadUrl,那么这将无法在浏览器中使用。您应该在浏览器中使用webContentLink并登录Google。