c# - 将 Google Drive .NET API 与 UWP 应用程序结合使用

标签 c# windows-runtime google-drive-api uwp

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

在模块 System.dll 中找不到类型 System.ComponentModel.ExpandableObjectConverter

我正在运行 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这是半相关的,除了答案只是它不会工作并且该帖子已经死了 4 年。我还想创建一个专门标记谷歌团队的新帖子(就像快速入门所说的那样)。

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

最佳答案

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

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

我只是尝试搜索这个但没有任何运气,但这里有一个建议,您可以使用 JavaScript 向 Drive API 发出请求。为此,您可以引用JavaScript Quickstart .然后你可以把它变成一个网络托管的UWP应用程序,更多信息,你可以引用Convert your web application to a Universal Windows Platform (UWP) app .

另一个可能使工作更容易的建议是使用 Rest API发送HTTP请求,也可以引用API Reference .

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

关于c# - 将 Google Drive .NET API 与 UWP 应用程序结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39734307/

相关文章:

event-handling - 恢复应用程序 Windows Phone

java - 如何授予 Google Drive REST API 的完全权限?

android - Google Drive android API 共享问题

c# - 将列表分割为多个列表时 System.Linq 的性能问题

c# - 如何使结构不可变?

c# - 将内存流写入文件

c# - 如何从 lambda 表达式获取值?

c# - FileIO.ReadTextAsync 有时会挂起

Windows Phone 8.1 WinRT 页面自定义转换

c# - 完全访问 Google Drive 上的特定文件夹