Quantcast
Viewing all articles
Browse latest Browse all 204402

Xamarin Forms upload/download files to Google Drive

Hi,

Pardon the ignorance but I am new to XF and am trying to wrap my head around this.

I had asked a previous question on uploading a file to OneDrive through my XF app. I got it to work and will post a solution.

The problem I am now facing is doing the same thing but for GoogleDrive. I have read much on how to do this and it seems very cumbersome. Has anyone successfully done this and if so is there a sample project any where.

After searching the web I did create a Drive API at console.developers.google.com

In my XF app I did this:

private static async Task SignInToGoogleDrive()
{
string[] Scopes = { DriveService.Scope.DriveReadonly, DriveService.Scope.DriveFile };

        GoogleWebAuthorizationBroker.Folder = "Google.Apis.Util.Store.FileDataStore";
        UserCredential credential;

        try
        {
            using (var stream = new FileStream(App.GoogleJsonFilePath, FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets, 
                    Scopes, "user", 
                    CancellationToken.None
                    );                   
            }

            // Create the service.
            var service = new DriveService(new Google.Apis.Services.BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "GarageMaster",
            });

            // Define parameters of request.
            FilesResource.ListRequest listRequest = service.Files.List();
            listRequest.PageSize = 10;
            listRequest.Fields = "nextPageToken, files(id, name)";

            // List files.
            IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
                .Files;
        }
        catch (Exception ex)
        {
            int testStop = 0;
        }

        int testStop1 = 0;
    }

I followed, what I think is correct, from the Google Api examples, and created a json file. I put it in my project and load it as a stream. That is what, in the above code, App.GoogleJsonFilePath is.

Now to be quite honest, I really don't know what this line does:

GoogleWebAuthorizationBroker.Folder = "Google.Apis.Util.Store.FileDataStore";

so I am going about this a bit blindly.

The above does compile, but when it gets to this line,
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(…..

flow goes to the Catch statement and the error is:

ex = {System.NotSupportedException: Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=6181........

I really don't know where to go from this point and any help would be appreciated.

Thanks.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>