无法从Google Drive API检索文件内容

时间:2012-12-18 13:11:18

标签: google-drive-api

我似乎无法通过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链接没有在没有摆弄参数的情况下返回响应?

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

解: 好的,我终于解决了。对于downloadURL上的GET请求,您需要在标头中发送访问令牌,即发送“授权:承载{您的访问令牌}”标头。请勿将访问令牌作为查询字符串的一部分发送。我在Drive SDK中使用的每个其他API调用都可以使用访问令牌作为查询字符串的一部分 - 除了这个。

2 个答案:

答案 0 :(得分:10)

好的,我终于解决了。对于downloadURL上的GET请求,您需要在标头中发送访问令牌,即发送“授权:承载{您的访问令牌}”标头。请勿将访问令牌作为查询字符串的一部分发送。我在Drive SDK中使用的每个其他API调用都可以使用访问令牌作为查询字符串的一部分 - 除了这个。

答案 1 :(得分:0)

尝试使用以下代码进行请求

var service1=SetCredential();
var AccessToken=((Google.Apis.Auth.OAuth2.UserCredential)service1.HttpClientInitializer).Token.AccessToken;
String link = "https://www.googleapis.com/drive/v2/files/" + fileId ;

HttpWebRequest request = WebRequest.Create(link) as HttpWebRequest;
request.Method = "GET";
request.Headers.Add("Authorization", "Bearer " + AccessToken);
WebResponse response = request.GetResponse();