Xamarin forms upload to google drive

2020-05-02 13:35发布

问题:

In my Xamarin forms application I need to upload a zip file to google drive.Is it possible to do this using Xamarin forms application? Do I need to implement this in each platform using dependency services? Please give me a proper suggestion.

回答1:

I'd imagine you'd have to create a REST service to make use of the Google Drive API, of course you'd have to manage authentication as well.



回答2:

Use Google's C#-based API Nuget, they support PCL-based projects and the API contains Google Drive support:

Xamarin.Forms:

<package id="Google.Apis" version="1.16.0" targetFramework="portable45-net45+win8+wp8" />

Xamarin.Android:

<package id="Google.Apis" version="1.16.0"    targetFramework="monoandroid60"` />

Xamarin.iOS:

<package id="Google.Apis" version="1.16.0" targetFramework="xamarinios10" />

From Google's Drive Sample:

GoogleWebAuthorizationBroker.Folder = "Drive.Sample";
UserCredential credential;
using (var stream = new System.IO.FileStream("client_secrets.json",
    System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None);
}

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

await UploadFileAsync(service);

Ref: https://developers.google.com/api-client-library/dotnet/apis/drive/v3