我正在使用驱动程序SDK作为我的下载应用程序,根据文档,我必须使用files.get()查询文件元数据,我在这里执行:
File remoteFile = this.driveClient.files().get(this.remoteMediaId).execute();
我给出的响应包含所有描述的元数据,包括downloadUrl:
{
"alternateLink" : "https://docs.google.com/file/d/0B9G3K3KUUcIISUdXMHkycUNFLVE/edit?usp=drive_web",
"createdDate" : "2013-04-29T21:32:06.009Z",
"downloadUrl" : "https://doc-04-9o-docs.googleusercontent.com/docs/securesc/kgbtvvv26q246qm8m1k4g2in1v7bhclv/e41n570nep6dionm41b8g25ennqqspmh/1367863200000/14985545053951403951/14985545053951403951/0B9G3K3KUUcIISUdXMHkycUNFLVE?h=16653014193614665626&e=download&gd=true",
...
然后我使用提供的下载链接为该文件发出http请求:
HttpResponse downloadResponse = null;
try {
log.info(downloadUrl);
log.info(downloadUrl.equals(remoteFile.getDownloadUrl()));
downloadResponse = this.driveClient.getRequestFactory()
.buildGetRequest(new GenericUrl(downloadUrl))
.execute();
...
大多数文件的响应是404,但有些工作。如果我根据文档运行curl请求,我也会得到404。
curl -v -H 'Authorization: Bearer ya29.***********...' 'https://doc-04-9o-docs.googleusercontent.com/docs/securesc/kgbtvvv26q246qm8m1k4g2in1v7bhclv/e41n570nep6dionm41b8g25ennqqspmh/1367863200000/14985545053951403951/14985545053951403951/0B9G3K3KUUcIISUdXMHkycUNFLVE?h=16653014193614665626&e=download&gd=true'
* About to connect() to doc-04-9o-docs.googleusercontent.com port 443 (#0)
* Trying 74.125.239.140...
* connected
* Connected to doc-04-9o-docs.googleusercontent.com (74.125.239.140) port 443 (#0)
...
> GET /docs/securesc/kgbtvvv26q246qm8m1k4g2in1v7bhclv/e41n570nep6dionm41b8g25ennqqspmh/1367863200000/14985545053951403951/14985545053951403951/0B9G3K3KUUcIISUdXMHkycUNFLVE?h=16653014193614665626&e=download&gd=true HTTP/1.1
> User-Agent: curl/7.27.0
> Host: doc-04-9o-docs.googleusercontent.com
> Accept: */*
> Authorization: Bearer ya29.***************************************
>
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 404 Not Found
当我将授权承载请求标题更改为我知道无效的内容时,例如。 “foobar”我得到401未经授权的回复。但是,当我使用有效密钥查询元数据时,我会返回下载URL和webcontent网址。我登录时可以在浏览器中看到该文件,但是downloadUrl失败了。 我在这里描述的程序如下所述:https://developers.google.com/drive/manage-downloads
更新: 如果我删除gd = true参数,我会得到一个带有位置标题的302到一个有效的链接。 downloadUrl是从谷歌回来的吗?
UPDATE2: 我在检索元数据中的downloadUrl后立即发出请求,我知道文档指出它是一个短命的URL。
UPDATE3: 作为参考,这是一个通过相同的方法检索的downloadUrl,它能够成功检索图像: https://doc-0s-9o-docs.googleusercontent.com/docs/securesc/kgbtvvv26q246qm8m1k4g2in1v7bhclv/q8d8m0pthrvjbje5srvraoe864okrb5r/1367870400000/14985545053951403951/14985545053951403951/0B9G3K3KUUcIIRWgxLVZpSzBfdWM?h=16653014193614665626&e=download&gd=true