0

我正嘗試使用Ruby on Rails應用創建/查看Google文檔。我認爲我能夠創建文檔,但是當我點擊從API回調中獲得的鏈接時,我得到一個錯誤401:需要登錄。Google Drive上的服務帳戶需要登錄

這裏是我使用創建文檔的代碼。

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 

有什麼建議嗎?

謝謝!

+0

有一個谷歌驅動器的寶石,似乎是很容易使用從我可以看到這裏。 – three

+0

有沒有辦法使用這個寶石獲得鏈接到谷歌文檔? – babaloo

+0

是的,這麼想吧,就是這樣:https://github.com/gimite/google-drive-ruby我用的是電子表格,很簡單。 – three

回答

1

如果您點擊的鏈接是downloadUrl,那麼這在瀏覽器中不起作用。您應該在瀏覽器中使用webContentLink並登錄Google。

相關問題