在UWP应用程序中使用Google Drive .NET API

问题描述:

我试图在我的UWP应用程序中使用Google Drive作为存储位置。我从Google提供的quickstart开始。我将代码复制到一个空白的UWP项目中,将一些输出代码(Console.Writeline更改为textbox.append方法),然后尝试构建它。它构建失败,并且报告错误:在UWP应用程序中使用Google Drive .NET API

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

我运行Windows 10和2015年VS和我已经安装过的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,使请求驱动API。要做到这一点,你可以参考JavaScript Quickstart。然后,您可以将其转换为网站托管的UWP应用程序,有关更多信息,请参阅Convert your web application to a Universal Windows Platform (UWP) app

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

作为@Vincent的最终建议是,如果您有权访问SDK代码,则还可以尝试使其适用于UWP。这意味着您需要修改此SDK的源代码。

+0

感谢您在@文森特的答案之上构建。我会尝试其他的API路线 - 猜猜最好迟到,而不是乱搞。感谢您花时间回答我的问题。 – Nibious

用于构建Windows Store/UWP应用程序的.Net风格比完整的.Net框架具有更少的功能。不幸的是,ExpandableObjectConverter对象不适用于UWP应用程序。

您需要Google提供的UWP SDK才能构建UWP应用程序。

如果您有权访问SDK代码,则还可以尝试使其适用于UWP。

+0

我找不到UWP SDK。感谢您帮助找到解决方案,并确认我没有完全成为一个白痴:-P – Nibious