使用 Google Drive .NET API 和 UWP 应用程序

Using Google Drive .NET API with UWP App

我正在尝试使用 Google 驱动器作为我的 UWP 应用程序中的存储位置。我从 Google 提供的 quickstart 开始。我将代码复制到一个空白的 UWP 项目中,更改一些输出代码(Console.Writelinetextbox.append 方法)并尝试构建它。构建失败报错:

Cannot find type System.ComponentModel.ExpandableObjectConverter in module System.dll

我是运行 Windows 10和VS 2015,我已经通过NuGet安装了sdk。快速入门中的示例代码确实适用于控制台应用程序。是 UWP 应用程序有问题。

对于 UWP 应用程序,我将快速启动代码放在按钮单击方法中。这是因为 API 实际上有一个用于 uwp 应用程序的异步方法,它与快速入门中给出的代码有点不同。

包括:

using System;
using System.Collections.Generic;
using System.IO;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Google.Apis.Auth.OAuth2; 
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.Threading;

按钮方法:

private async void button_Click(object sender, RoutedEventArgs e)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = ""; //System.Environment.GetFolderPath(
                                             //System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");

                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    new Uri("ms-appx:///Assets/client_secrets.json"),
                    Scopes,
                    "user",
                    CancellationToken.None);
                //Console.WriteLine("Credential file saved to: " + credPath);
            }

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

            // 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;
            textBox.Text += "Files:\n";
            if (files != null && files.Count > 0)
            {
                foreach (var file in files)
                {
                    textBox.Text += (file.Name + file.Id + "\n");
                }
            }
            else
            {
                textBox.Text += ("No files found.");
            }
        }

应用编译后测试代码将无法运行,因为它缺少加载客户端密码的代码。由于我无法测试代码,因此我只能提供这些。

还有一个post是半相关的,只是答案是它不会起作用,post已经死了4年了。我还想创建一个新的 post 来专门标记 google 团队(就像快速入门所说的那样)。

我的具体问题是:是否有解决此问题的方法,还是我只是做错了?

用于构建 Windows Store/UWP 应用程序的 .Net 风格的功能少于完整的 .Net 框架。遗憾的是,ExpandableObjectConverter 对象不可用于 UWP 应用程序。

您需要 Google 的 UWP SDK 来构建 UWP 应用程序。

如果您有SDK代码,也可以尝试适配UWP。

我同意@Vincent 的观点,UWP 应用程序使用 COM 作为基础并从那里构建。并非所有.Net API 都可以在UWP 应用程序中使用,此SDK 基于.Net APIs,这就是为什么您的控制台应用程序可以,但您的UWP 应用程序已关闭。对于它们之间的区别,这里有一个great answer来解释这个问题。但是,

"You will need an UWP SDK from Google to build an UWP applications."

我只是尝试搜索这个但没有成功,但这里有一个建议,您可以使用 JavaScript 向驱动器 API 发出请求。为此,您可以参考JavaScript Quickstart. Then you can turn it to a web hosted UWP app, for more information, you can refer to Convert your web application to a Universal Windows Platform (UWP) app.

另一个可能使工作更轻松的建议是使用 Rest API to send HTTP requests, you can also refer to API Reference

最后的建议如@Vincent所说,如果你有SDK代码,你也可以尝试适配UWP。这意味着你需要修改这个SDK的源代码。