获取Google云端硬盘API中的导出链接

时间:2015-08-24 21:14:46

标签: ruby google-api google-api-ruby-client

我正在使用Google云端硬盘客户端,并尝试将某些工作表文档导出为CSV格式。我可以正常使用我的文件,但在尝试访问exportLinks时,它似乎不存在于我的文件中,即使它确实在Google网站的API资源管理器中返回它。以下是我尝试获取exportLink的方法:

client = Google::APIClient.new(:application_name => APPLICATION_NAME)
client.authorization = authorize
drive_api = client.discovered_api('drive', 'v2')

result = client.execute!(
               :api_method => drive_api.files.get,
               :parameters => {:fileId => "1MBP9Q9Q-9ZgLoYnY8ExS-EcxHLESI_vcK4J91ngp6-Q"})
file = result.data
puts("Fetched #{file["title"]}")
puts("Getting downloadURL")
exportLinks = file['exportLinks']['text/csv']

我不断收到错误说"未定义的方法' []' for nil:NilClass(NoMethodError)。'所以由于某种原因,我没有收到exportLinks返回的任何内容。有谁知道这是为什么?方法是否已更改,但尚未在API Explorer上更新?我还犯了其他一些愚蠢的错误吗?我在某个地方错过了一个逗号吗?非常感谢任何帮助!

如果您需要更多代码,请与我们联系。

编辑 - 堆栈跟踪:

/usr/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/billluhmann/Developer/OpinionLab-Automation/autoimport.rb
Doing some Google Drive stuff
Fetched Test Spreadsheet
Getting downloadURL
/Users/billluhmann/Developer/OpinionLab-Automation/autoimport.rb:163:in     `main': undefined method `[]' for nil:NilClass (NoMethodError)
from /Users/billluhmann/Developer/OpinionLab-Automation/autoimport.rb:237:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'

Process finished with exit code 1

编辑2:我注销了我得到的回复......而且确实没有包含exportLinks。

以下是我的应用程序看到的(相关部分)响应:

"parents": [
{
 "kind": "drive#parentReference",
 "id": "0AJj4OUAR-EvNUk9PVA",
 "selfLink": "https://www.googleapis.com/drive/v2/files/1MBP9Q9Q-9ZgLoYnY8ExS-EcxHLESI_vcK4J91ngp6-Q/parents/0AJj4OUAR-EvNUk9PVA",
 "parentLink": "https://www.googleapis.com/drive/v2/files/0AJj4OUAR-EvNUk9PVA",
 "isRoot": true
}
],
"userPermission": {
"kind": "drive#permission",
"etag": "\"OAy0CkbWKifm-WnYBAdgx1dwNXA/IGgJsRJ105V1BXysjoqMdW7TLx8\"",
"id": "me",
"selfLink": "https://www.googleapis.com/drive/v2/files/1MBP9Q9Q-9ZgLoYnY8ExS-EcxHLESI_vcK4J91ngp6-Q/permissions/me",
"role": "owner",
"type": "user"
},   

响应API Explorer告诉我应该得到:

"parents": [
{

"kind": "drive#parentReference",
"id": "0AJj4OUAR-EvNUk9PVA",
"selfLink": "https://www.googleapis.com/drive/v2/files/1MBP9Q9Q-9ZgLoYnY8ExS-EcxHLESI_vcK4J91ngp6-Q/parents/0AJj4OUAR-EvNUk9PVA",
"parentLink": "https://www.googleapis.com/drive/v2/files/0AJj4OUAR-EvNUk9PVA",
"isRoot": true
}
],
"exportLinks": {
"text/csv": "https://docs.google.com/spreadsheets/export?id=1MBP9Q9Q-9ZgLoYnY8ExS-EcxHLESI_vcK4J91ngp6-Q&exportFormat=csv",
"application/pdf": "https://docs.google.com/spreadsheets/export?id=1MBP9Q9Q-9ZgLoYnY8ExS-EcxHLESI_vcK4J91ngp6-Q&exportFormat=pdf",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "https://docs.google.com/spreadsheets/export?id=1MBP9Q9Q-9ZgLoYnY8ExS-EcxHLESI_vcK4J91ngp6-Q&exportFormat=xlsx"
},
"userPermission": {
"kind": "drive#permission",
"etag": "\"OAy0CkbWKifm-WnYBAdgx1dwNXA/IGgJsRJ105V1BXysjoqMdW7TLx8\"",
"id": "me",
"selfLink": "https://www.googleapis.com/drive/v2/files/1MBP9Q9Q-9ZgLoYnY8ExS-EcxHLESI_vcK4J91ngp6-Q/permissions/me",
"role": "owner",
"type": "user"
}, 

1 个答案:

答案 0 :(得分:0)

不确定是否有这个帮助,但是在我尝试使用google驱动器api的共享google工作表的共享设置界面中,如果我选中Disable options to download, print, and copy for commenters and viewers复选框,则谷歌的json响应将不会包括exportLinks条目