Google Drive API using C# - Uploading Google Drive API using C# - Uploading asp.net asp.net

Google Drive API using C# - Uploading


FileDatastore puts files by default in %appData%. Normally I would do something like. (lets assume I use "user" as well)

new FileDataStore("Console.Analytics.Auth.Store")  

Then when I call it I get a directory called

%AppData%\Roaming\Console.Analytics.Auth.Store

in that directory now lives a file called

Google.Apis.Auth.OAuth2.Responses.TokenResponse-user

I haven't tested what doing

new FileDataStore(Server.MapPath("UploadedPdf/"))

will do but my guess is you are going to get a directory named

%AppData%\Roaming\serverpat/uploadpdf/

Which I don't think is what you are after. If that is actually what you are trying to do that you have made that directory writeable? You might want to lookinto using LocalFileDataStore instead.

I am not sure if this is your problem or not.

Also remember you are hard coding "user" so technically your code is going to ask you to authenticate once for "user" after that it has the authentication in that file for "user" so there is no reason to ask for it again.


I think you are calling the AuthorizeAsync() wrong. Use the await keyword and remote the .Result. You'll have to mark the method as async in order to do this.

I would also suggest using the UploadAsync() method instead. You don't need to load the entire file into memory with ReadAllBytes() either. Just use File.OpenRead() and pass the FileStream instead of the MemoryStream.

Google has some example code:https://developers.google.com/api-client-library/dotnet/guide/media_upload


Your code suggests that you are using "Installed Application" workflow/credentials, which is supposed to work fine if you are running your ASP.NET application locally. However, when you move the same code to a web server, its no more an installed application. It enters the category of "Web Application".

You need to go to Google Developer Console->Credentials->Create New Client ID and generate a new set of credentials for your web application. Also you need to change the OAuth Flow as per this guide:

https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-aspnet-mvc