Reputation: 322
I have been using a Google Drive SDK integration since October. It has worked fine since then, and I have not modified the code in the project since I got it working. Today, for some unknown reason, every request to download or upload a file to the drive returns an error:
The remote server returned an error (401) not authorized.
The section of code I am currently working on is:
public static Stream GetDownloadStream(string id)
{
File file = DriveService.Files.Get(id).Fetch();
if (!String.IsNullOrEmpty(file.DownloadUrl))
{
Stream stream;
var request = (HttpWebRequest) WebRequest.Create(
new Uri(file.DownloadUrl));
Auth.ApplyAuthenticationToRequest(request);
try
{
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
stream = response.GetResponseStream();
}
else
{
throw new Exception(response.StatusDescription);
}
return stream;
}
catch(Exception e)
{
GetCredentials();
return GetDownloadStream(id);
}
}
throw new Exception("File Cannot Be Downloaded.");
}
The error is returned at the request.GetResponse() method call. The DriveService properly loads the file, and the file has what appears to be a properly formed DownloadURL. Everything about the DriveService seems to work in the rest of the code, it searches and retrieves file info properly, but for some reason, it won't download or upload. Has there been some change to the format of the DownloadURL at the google server end that I need to incorporate into my code?
Upvotes: 1
Views: 1724
Reputation: 27748
Have you disabled web security? It seems google drive is checking that one.
--disable-web-security
If so, enable web security. It worked for me.
Upvotes: 1
Reputation: 1102
Some time downloadurl is expired so that time create new request and download file again. also because of incorrect refresh token and access token.
Upvotes: 0