11

I am trying to use Google Drive API from an asp.net application to upload files.

Problem: The code works locally but when uploaded to server nothing happens (the page just keeps loading...no consent screen showing. Help appreaciated. "UploadedPdf" folder is writable and client_secrets.json exists in the folder.

NOTE: I have not installed anything on the server but just uploaded all the files included the Google API dll's into the bin folder of the server.

UserCredential credential;
        using (var filestream = new FileStream(Server.MapPath("UploadedPdf/client_secrets.json"),
            FileMode.Open, FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(filestream).Secrets,
                new[] { DriveService.Scope.Drive },
                "user",
                CancellationToken.None,
                new FileDataStore(Server.MapPath("UploadedPdf/"))).Result;
        }

        // Create the service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Drive API Sample",
        });

        Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
        body.Title = "My document";
        body.Description = "A test document";
        body.MimeType = "text/plain";

        byte[] byteArray = System.IO.File.ReadAllBytes(Server.MapPath("UploadedPdf/sample.txt"));
        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
        request.Upload();

        Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
pessi
  • 681
  • 1
  • 10
  • 26

4 Answers4

2

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

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

Bryan
  • 2,775
  • 3
  • 28
  • 40
0

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

Furhan S.
  • 1,494
  • 2
  • 13
  • 22
  • Then try the authentication method provided in the link above rather than what you were trying earlier and share the results please. – Furhan S. Jul 09 '15 at 22:28
  • Another flaw may be as pointed out by DalmTo. You may want to extend the storage interface to implement your own. Reference: https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#credentials – Furhan S. Jul 09 '15 at 22:35
0

steps include

1)Access to the internet and a web browser, in order to authorize the sample   app.
2)A Google account with Drive enabled.
3)An environment to run programs in your selected language.

try this,

https://developers.google.com/drive/v2/reference/files/insert#examples