【发布时间】:2016-09-27 21:05:32
【问题描述】:
我正在尝试在我的 UWP 应用程序中使用 Google Drive 作为存储位置。我从谷歌提供的quickstart 开始。我将代码复制到一个空白的 UWP 项目中,将一些输出代码(Console.Writeline 更改为 textbox.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 是半相关的,只是答案是它不起作用并且帖子已经死了 4 年。我还想创建一个专门标记谷歌团队的新帖子(就像快速入门所说的那样)。
我的具体问题是:是否有解决此问题的方法,或者我只是做错了?
【问题讨论】:
标签: c# windows-runtime google-drive-api uwp