【发布时间】:2019-05-14 22:25:27
【问题描述】:
在我的 Xamarin 表单应用程序中,我需要将 zip 文件上传到谷歌驱动器。是否可以使用 Xamarin 表单应用程序执行此操作?我是否需要使用依赖服务在每个平台上实现它?请给我一个合适的建议。
【问题讨论】:
标签: xamarin xamarin.ios xamarin.android xamarin.forms
在我的 Xamarin 表单应用程序中,我需要将 zip 文件上传到谷歌驱动器。是否可以使用 Xamarin 表单应用程序执行此操作?我是否需要使用依赖服务在每个平台上实现它?请给我一个合适的建议。
【问题讨论】:
标签: xamarin xamarin.ios xamarin.android xamarin.forms
我想你必须创建一个REST 服务才能使用Google Drive API,当然你还必须管理authentication。
【讨论】:
使用 Google 基于 C# 的 API Nuget,它们支持基于 PCL 的项目,并且 API 包含 Google Drive 支持:
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" />
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);
参考:https://developers.google.com/api-client-library/dotnet/apis/drive/v3
【讨论】: