Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API using C# - Uploading

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;
like image 590
pessi Avatar asked Jul 01 '15 16:07

pessi


1 Answers

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.

like image 56
DaImTo Avatar answered Oct 21 '22 09:10

DaImTo