2016-09-27 101 views
1

我試圖在我的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年。我也想創建一個專門標記谷歌團隊的新帖子(就像快速入門說的那樣)。

我的具體問題是:有沒有解決這個問題或我只是做錯了這個?

回答

1

我同意@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

1

用於構建Windows Store/UWP應用程序的.Net風格比完整的.Net框架具有更少的功能。不幸的是,ExpandableObjectConverter對象不適用於UWP應用程序。

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

如果您有權訪問SDK代碼,則還可以嘗試使其適用於UWP。

+0

我找不到UWP SDK。感謝您幫助找到解決方案,並確認我沒有完全成爲一個白癡:-P – Nibious

相關問題