无法从Google Drive API检索文件内容 [英] Unable to retrieve file content from google drive API
问题描述
我似乎无法通过Google Drive SDK检索文件的内容。要重现此问题,我使用 API资源管理器来获取一个小文本文件的元数据:
200 OK
- 显示标题 -
{
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,
查看: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_Nu
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请求,您似乎需要在头中发送访问令牌,即发送一个Authorization:Bearer {您的访问令牌}头。不要将访问令牌作为查询字符串的一部分发送。在Drive SDK中使用的每个其他API调用都可以使用访问令牌作为查询字符串的一部分 - 除了这一个。
好吧,我终于搞定了。对于downloadURL上的GET请求,您似乎需要在头中发送访问令牌,即发送一个Authorization:Bearer {您的访问令牌}头。不要将访问令牌作为查询字符串的一部分发送。在Drive SDK中使用的每个其他API调用都可以使用访问令牌作为查询字符串的一部分 - 除此之外。
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
}
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).
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
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.
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屋!