问题
I have problem on getting files from downloadUrl provided from Google Drive API v2.
Here is the screenshot.
I have tried several websites and it still occurs. How can I fix it? Or is it a problem from Google?
20200312 Edit: I am using Javascript for Google File Picker and it returns JSON for the picked file. Then I use Classic ASP to get the file to server and save.
Here is the code
Front-end
function initGooglePicker() {
var picker = new FilePicker({
apiKey: API_KEY,
clientId: CLIENT_ID,
buttonEl: document.getElementById('btnGoogleDrive'),
onSelect: function(file, access_token) {
var path, ext, filename;
if (file.downloadUrl) {
path = file.downloadUrl;
filename = file.title;
ext = filename.split('.').pop().toLowerCase();
}
else if (file.exportLinks) {
if (file['exportLinks']['application/pdf']) {
path = file['exportLinks']['application/pdf'] + "&exportFormat=pdf" ;
ext = "pdf";
filename = file.title + "." + ext;
}
}
$('#access_token').val(access_token);
},
onCancel: function() {}
});
}
Back-end
Function UploadGoogleDriveFile(fromPath, toPath, accessToken)
Dim retVal
retVal = "0"
Dim objXML
Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
objXML.Open "GET", fromPath, False
objXml.SetRequestHeader "Authorization", "Bearer " & accessToken
objXML.Send
If objXML.readystate = 4 Then
Response.Write(objXML.status & "<br>")
Response.Write(objXML.responseBody)
Response.End
End If
If objXML.readystate = 4 And objXML.status = 200 Then
If Len(objXML.responseText) > acct_cv_file_size Then
retVal = "2"
Else
CreateFolderBeforeUpload(toPath)
Set objStream = Server.createobject("Adodb.Stream")
objStream.Type = 1
objStream.Open
objStream.Write objXML.responseBody
objStream.SaveToFile toPath, 2
objStream.Close
Set objStream = Nothing
retVal = "1"
End If
End If
Set objXML = Nothing
UploadGoogleDriveFile = retVal
End Function
回答1:
If you want to download a file using the Drive API V2 and setting the access token in the header, you have to do it by calling this url:
https://www.googleapis.com/drive/v2/files/[file-Id]?alt=media
As it's told in the Response section in the Files: get endpoint. It's really important the url parameter alt=media
in order to make it work.
I've used this code before (works for me), which I found on Issue Tracker, in that way you can have another approach, too.
//fileurl - https://www.googleapis.com/drive/v2/files/[file-Id]?alt=media
//accessToken - client's accessToken
//filepath - location to store the downloaded file
public void downloadGdriveFileThroughWebRequest(string fileurl, string filepath, string accessToken)
{
HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(fileurl);
rq.Method = "GET";
rq.PreAuthenticate = true;
rq.Headers.Add("Authorization", "Bearer " + accessToken);
try {
HttpWebResponse resp = (HttpWebResponse)rq.GetResponse();
using (Stream output = File.OpenWrite(filepath))
{
using (Stream s = resp.GetResponseStream())
{
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = s.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
}
catch (WebException ex)
{
using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
reader.ReadLine();
Console.WriteLine(reader.ReadToEnd());
}
}
}
来源:https://stackoverflow.com/questions/60595512/i-cannot-get-the-file-with-short-lived-url-downloadurl-from-google-drive-api