karishmal / google-api-dotnet-client

Automatically exported from code.google.com/p/google-api-dotnet-client
Apache License 2.0
0 stars 0 forks source link

Download fails in Drive.Sample #387

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Download the latest of GoogleApisSamples
2. Enable Drive API and configure ClientID and Client Secret
3. Run the Drive.Sample project after setting the UploadFileName and 
DownloadDirectoryName constants

What is the expected output? 
The file is uploaded to Google Drive
The file is downloaded from Google Drive
The file is removed from Google Drive

What do you see instead?
The file is uploaded to Google Drive
The program fails while attempting to download the file from Google Drive
The file is removed from Google Drive

What version of the product are you using? Hg clone from 8/23/2013
What is your operating system? Win 7 x64
What is your IDE? Visual Studio 2012
What is the .NET framework version? .NET Framework 4

Additional information:
The app uploads an image to Google Drive first. When it attempts the download 
it fails in the MediaDownloader.cs file, inside the DownloadCore method. This 
is after I have provided the ClientID and Client Secret.

When running it in the debugger. the response comes back as:
{StatusCode: 302, ReasonPhrase: 'Moved Temporarily'

We can see a bunch of redirects are taking place in the log file too and it 
reports Abnormal response at the end (query strings omitted for brevity):

013-08-23 16:19:18,950 [13] DEBUG Google.Apis.Http.ConfigurableMessageHandler 
(null) - Redirect response was handled successfully. Redirect to 
https://docs.google.com/a/...

2013-08-23 16:19:19,173 [13] DEBUG Google.Apis.Http.ConfigurableMessageHandler 
(null) - Redirect response was handled successfully. Redirect to 
https://doc-0s-1g-docs.googleusercontent.com/docs/securesc/...

2013-08-23 16:19:19,175 [13] DEBUG Google.Apis.Http.ConfigurableMessageHandler 
(null) - Abnormal response is being returned. Status Code is Redirect

A fix or suggestions/pointers on how to debug this kind of issue will be 
greatly appreciated.

Original issue reported on code.google.com by u...@ftitech.mygbiz.com on 23 Aug 2013 at 3:46

GoogleCodeExporter commented 9 years ago
In the .NET Client library, MediaDownloader.cs, DownloadCore method, i 
commented out builder.AddParameter(RequestParameterType.Query, "alt", "media"); 
and that appears to have resolved the download issue. Could someone point me to 
documentation explaining the purpose of this parameter or why commenting out 
this line would resolve the issue? 

Original comment by u...@ftitech.mygbiz.com on 26 Aug 2013 at 2:31

GoogleCodeExporter commented 9 years ago
You should use "alt"="media", because only in that way you will get the file 
itself and not its metadata.

Can you try again? The sample works for me.
If it still doesn't work, can you please attach Fiddler output to this Issue?

Thanks

Original comment by pele...@google.com on 27 Aug 2013 at 1:14

GoogleCodeExporter commented 9 years ago

Original comment by pele...@google.com on 27 Aug 2013 at 1:14

GoogleCodeExporter commented 9 years ago
Hi Peleyal,
For some reason, without the "alt"="media" parameter I was able to download the 
file. 
As you requested, I'll send you Fiddler output with and without the parameter 
added to the code.

Original comment by u...@ftitech.mygbiz.com on 27 Aug 2013 at 1:31

GoogleCodeExporter commented 9 years ago
I've attached two Fiddler traces containing the activity around initiating the 
download:
- SuccessfulUploadFailedDownloadMediaParamSetBrief.saz showing the failed 
download and 302 redirect. A 1k file placeholder is created on the target, but 
the file is empty. In this case, the media parameter is set.
- SuccessfulUploadAndDownloadMediaParamRemovedBrief.saz showing the successful 
download. I am able to open the file on the target following the download. In 
this case, the media parameter is not set.

Original comment by u...@ftitech.mygbiz.com on 27 Aug 2013 at 2:21

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by pele...@google.com on 27 Aug 2013 at 7:22

GoogleCodeExporter commented 9 years ago
Can you please check what happen if you change the number of tries in the 
HttpConfigurableHandler?
You can do so, by the following snippet:

        class MyInitializer : IConfigurableHttpClientInitializer
        {
            public void Initialize(ConfigurableHttpClient httpClient)
            {
                httpClient.MessageHandler.NumTries = 10;
            }
        }

and when you create your service, add this initializer, as following:

        var service = new DriveService(new BaseClientService.Initializer()
        {
          Authenticator = auth,
          ApplicationName = "YOUR_APP_NAME_HERE",
          HttpClientInitializer = new MyInitializer();
        });

If it will work, we may add another parameter to HttpConfigurableMessageHandler 
that will store the number of redirect allowed.

Thanks,
looking forward to your response.

Original comment by pele...@google.com on 14 Sep 2013 at 1:18

GoogleCodeExporter commented 9 years ago

Original comment by pele...@google.com on 14 Sep 2013 at 1:18

GoogleCodeExporter commented 9 years ago
https://codereview.appspot.com/13480044/

Original comment by pele...@google.com on 14 Sep 2013 at 1:52

GoogleCodeExporter commented 9 years ago
To start, we implemented the change you suggested (setting NumTries to 10 in 
the MessageHandler) and that seemed to do the trick with the 
builder.AddParameter(RequestParameterType.Query, "alt", "media") code in place 
in the MediaDownloader. Thank you for your help with this.
Can you explain why setting NumTries to 10 would resolve the issue? Is it 
because there were more redirects that occur during download that needed to be 
handled? If so, is there a static number to be expected or is it based on the 
size of the download?
Finally, on a related note, does the MediaDownloader work with Google Docs that 
actually require export to some format for download or do we have to stick with 
the Google Docs API that has been deprecated?

Thanks,
Ethan

Original comment by ethanwil...@gmail.com on 18 Sep 2013 at 8:34

GoogleCodeExporter commented 9 years ago
Exactly. They were too many redirects (more than 3). The current implementation 
stops after only 3 tries. The attached CL 
(https://codereview.appspot.com/13480044/) add another property 
(NumRedirectTries).

You can review that CL, and let me know your thoughts.

Regarding your second question, the above CL contains a solution for that as 
well.

Let me know and thanks for your feedback

Original comment by pele...@google.com on 18 Sep 2013 at 8:56

GoogleCodeExporter commented 9 years ago
Thanks for the update, Peleyal. Your changes look fine and we will get the 
latest. Returning to my second question, I didn't see anything in the code that 
indicates how you are specifically handling Google Docs that must be exported 
to some format, such as Doc or PDF prior to being downloaded. It appears that 
the only way to do that is to use the deprecated Google Docs API, as shown in 
the download code on this page: 
https://developers.google.com/google-apps/documents-list/#downloading_documents_
and_files. 

Original comment by ethanwil...@gmail.com on 19 Sep 2013 at 3:53

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I'm not exactly familiar with the old way the docs work, but....

I checked in Google Explorer API how a file looks like. This is the output:

kind": "drive#file",
"id": "ID",
"selfLink": "https://content.googleapis.com/drive/v2/files/[ID]",
...
"title": "Introduction to the .NET client library",
"mimeType": "application/vnd.google-apps.presentation",
"labels": {
"starred": false,
"hidden": false,
"trashed": false,
"restricted": false,
"viewed": true
},
"createdDate": "2013-09-01T20:44:32.507Z",
"modifiedDate": "2013-09-05T15:11:46.149Z",
"modifiedByMeDate": "2013-09-05T15:11:46.149Z",
"lastViewedByMeDate": "2013-09-19T13:10:43.754Z",
"parents": [
{
"kind": "drive#parentReference",
"id": "0AKSq4G25HO_JUk9PVA",
"selfLink": 
"https://content.googleapis.com/drive/v2/files/ID/parents/PARENT_ID",
"parentLink": "https://content.googleapis.com/drive/v2/files/PARENT_ID",
"isRoot": true
}
],
"exportLinks": {
"text/plain": "
https://docs.google.com/feeds/download/presentations/Export?id=ID&exportFormat=t
xt
",
"application/pdf": "
https://docs.google.com/feeds/download/presentations/Export?id=ID&exportFormat=p
df
",
"application/vnd.openxmlformats-officedocument.presentationml.presentation":
"
https://docs.google.com/feeds/download/presentations/Export?id=ID&exportFormat=p
ptx
"
},
...
"editable": true,
"copyable": true,
"writersCanShare": true,
"shared": true,
"appDataContents": false
},

As you can see the exportLinks contains a link to download the file as txt,
pdf, pptx. With my fix (In the mentioned CL) you will be able to download
those file using the MediaDownloader.

Does that answer your question?

Eyal

Original comment by pele...@google.com on 19 Sep 2013 at 5:01

GoogleCodeExporter commented 9 years ago
Yes and thanks again, Eyal. I sincerely appreciate your help.
Ethan

Original comment by ethanwil...@gmail.com on 19 Sep 2013 at 6:06

GoogleCodeExporter commented 9 years ago
NP.
Let me know if you have any other questions or suggestions.

thanks!
Eyal

Original comment by pele...@google.com on 19 Sep 2013 at 6:14

GoogleCodeExporter commented 9 years ago

Original comment by pele...@google.com on 16 Oct 2013 at 10:05