2

我正在使用 Google Drive Ruby 客户端并尝试将一些工作表文档导出为 CSV 格式。我可以很好地获取我的文件,但是当我尝试访问exportLinks它时,它似乎并不存在于我的文件中,即使它确实在 Google 网站上的 API Explorer 中返回了它。这是我试图获得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']

我不断收到一条错误消息,提示“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"
}, 
4

1 回答 1

0

不确定这是否有帮助,但在我尝试使用 google drive api 的共享 google 工作表的共享设置 UI 中,如果我选中Disable options to download, print, and copy for commenters and viewers复选框,那么来自 google 的 json 响应将不包括 exportLinks 条目

于 2015-11-05T01:43:50.333 回答