我似乎无法通过 google drive SDK 检索文件的内容。为了重现这个问题,我使用API Explorer来获取一个小文本文件的元数据:
200 OK
- Show headers -
{
"kind": "drive#file",
"id": "0B75zvzRT_NusaDBtSTVmWWk1cVk",
"etag": "\"B6kWtzuiQYCrVi2MWyRaub0CRyo/MTM1NTgzNDEzMjU5MA\"",
"selfLink": "https://www.googleapis.com/drive/v2/files/0B75zvzRT_NusaDBtSTVmWWk1cVk",
"webContentLink": "https://docs.google.com/uc?id=0B75zvzRT_NusaDBtSTVmWWk1cVk&export=download",
"alternateLink": "https://docs.google.com/file/d/0B75zvzRT_NusaDBtSTVmWWk1cVk/edit",
"iconLink": "https://ssl.gstatic.com/docs/doclist/images/icon_10_text_list.png",
"thumbnailLink": "https://lh4.googleusercontent.com/ntOMGJ9Is5KigDUzpOe6xKRqVqHPyeeXPImG1vZTF2FDM5YOyeCZT62FsdBWTnetBA=s220",
"title": "sync.txt",
"mimeType": "text/plain",
"description": "description",
"labels": {
"starred": false,
"hidden": false,
"trashed": false,
"restricted": false,
"viewed": true
},
"createdDate": "2012-12-18T11:49:21.710Z",
"modifiedDate": "2012-12-18T12:35:32.590Z",
"modifiedByMeDate": "2012-12-18T12:35:32.590Z",
"lastViewedByMeDate": "2012-12-18T12:35:32.416Z",
"parents": [
{
"kind": "drive#parentReference",
"id": "0B75zvzRT_NusdVVJeGk2dVc2VHM",
"selfLink": "https://www.googleapis.com/drive/v2/files/0B75zvzRT_NusaDBtSTVmWWk1cVk/parents/0B75zvzRT_NusdVVJeGk2dVc2VHM",
"parentLink": "https://www.googleapis.com/drive/v2/files/0B75zvzRT_NusdVVJeGk2dVc2VHM",
"isRoot": false
}
],
"downloadUrl": "https://doc-0g-0-docs.googleusercontent.com/docs/securesc/b2hod7vud4bdud0ju4mut5hh2assmdju/rmi0iqd62g0im724ngmc5uva7femfffo/1355832000000/00903399969355284739/00903399969355284739/0B75zvzRT_NusaDBtSTVmWWk1cVk?h=16653014193614665626&e=download&gd=true",
"userPermission": {
"kind": "drive#permission",
"etag": "\"B6kWtzuiQYCrVi2MWyRaub0CRyo/kH0lkP-s4aFu1o5itR2fFqyLM6o\"",
"id": "me",
"selfLink": "https://www.googleapis.com/drive/v2/files/0B75zvzRT_NusaDBtSTVmWWk1cVk/permissions/me",
"role": "owner",
"type": "user"
},
"originalFilename": "sync.txt",
"fileExtension": "txt",
"md5Checksum": "ecd21579645508d1c206d5e6e20fd101",
"fileSize": "156",
"quotaBytesUsed": "156",
"ownerNames": [
"Sam Smith"
],
"lastModifyingUserName": "Sam Smith",
"editable": true,
"writersCanShare": true
}
然后我单击downloadUrl 链接,我总是得到一个空响应,即200 响应与一个空的正文。如果我从 URL 的末尾删除“gd=true”参数,它会很好地下载。在我的程序中,除了删除“gd = true”也不起作用(可能是因为程序在服务器上运行,因此我没有登录到我的帐户)之外,同样的事情发生了。
我的主要问题是为什么 downloadUrl 链接在不摆弄参数的情况下不返回响应?
更新:发现其他人在此处、此处和此处报告完全相同的问题
解决方案:好的,我终于解决了。对于downloadURL 上的GET 请求,您需要在标头中发送访问令牌,即发送“授权:承载{您的访问令牌}”标头。不要将访问令牌作为查询字符串的一部分发送。我在 Drive SDK 中使用的所有其他 API 调用都可以使用访问令牌作为查询字符串的一部分 - 除了这个。