我試圖在我的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年。我也想創建一個專門標記谷歌團隊的新帖子(就像快速入門說的那樣)。
我的具體問題是:有沒有解決這個問題或我只是做錯了這個?
感謝您在@文森特的答案之上構建。我會嘗試其他的API路線 - 猜猜最好遲到,而不是亂搞。感謝您花時間回答我的問題。 – Nibious