无法从 google drive API 检索文件内容 [英] Unable to retrieve file content from google drive API

查看:12
本文介绍了无法从 google drive API 检索文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法通过 google drive SDK 检索文件的内容.为了重现问题,我使用 API explorer获取小文本文件的元数据:

I seem to be unable to retrieve the content of a file via the google drive SDK. To reproduce the problem I use the API explorer to get the meta data for a small text file:

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"也不起作用(可能是因为程序在服务器上运行,因此我没有登录到我的帐户)之外,同样的事情发生了.

I then click on the downloadUrl link and I always get an empty response i.e a 200 response with an empty body. If I remove the "gd=true" parameter from the end of the Url it downloads fine. In my program the same thing happens except removing the "gd=true" also does not work (probably because the program runs on a server and therefore I am not logged into my account).

我的主要问题是为什么 downloadUrl 链接在不摆弄参数的情况下不返回响应?

My main question is why does the downloadUrl link not return a response without fiddling with the parameters?

更新:发现其他人报告完全相同的问题 这里这里这里

UPDATE: Found other people reporting the exact same issue Here, Here and Here

解决方案:好的,我终于解决了.对于 downloadURL 上的 GET 请求,您需要在标头中发送访问令牌,即发送Authorization: Bearer {your access token}"标头.不要将访问令牌作为查询字符串的一部分发送.我在 Drive SDK 中使用的所有其他 API 调用都可以将访问令牌作为查询字符串的一部分 - 除了这个.

SOLUTION: OK I finally worked it out. It seems for the GET request on the downloadURL you need to send the access token in the headers i.e send a "Authorization: Bearer {your access token}" header. DO NOT send the access token as part of the query string. Every other API call I have used in the Drive SDK is fine with the access token as part of the query string - except this one.

推荐答案

好的,我终于解决了.对于 downloadURL 上的 GET 请求,您需要在标头中发送访问令牌,即发送Authorization: Bearer {your access token}"标头.不要将访问令牌作为查询字符串的一部分发送.我在 Drive SDK 中使用的所有其他 API 调用都可以将访问令牌作为查询字符串的一部分 - 除了这个.

OK I finally worked it out. It seems for the GET request on the downloadURL you need to send the access token in the headers i.e send a "Authorization: Bearer {your access token}" header. DO NOT send the access token as part of the query string. Every other API call I have used in the Drive SDK is fine with the access token as part of the query string - except this one.

这篇关于无法从 google drive API 检索文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆