在UWP App中使用Google Drive .NET API [英] Using Google Drive .NET API with UWP App

查看:99
本文介绍了在UWP App中使用Google Drive .NET API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Google云端硬盘用作UWP应用程序中的存储位置.我从Google提供的 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.");
            }
        }

编译应用后,测试代码将无法正常工作,因为它缺少加载客户端机密的代码.由于我无法测试代码,因此我可以提供所有这些.

还有另一条帖子这是半相关的,只不过答案是它无法正常工作且该职位已死亡4年.我还想创建一个新帖子,专门标记Google团队(如quickstart所说的那样).

我的具体问题是:是否可以解决此问题,或者我做错了吗?

解决方案

我同意@ Vincent,UWP应用程序使用COM作为基础并从那里构建.并非所有.Net API都可以在UWP应用程序中使用,该SDK基于.Net API,这就是为什么您的控制台应用程序可以,但是您的UWP应用程序关闭的原因.对于它们之间的差异,这是一个

我只是尝试没有任何运气来搜索此内容,但这是一个建议,您可以使用JavaScript向Drive API发出请求.为此,您可以参考 JavaScript快速入门.然后,您可以将其转换为Web托管的UWP应用,有关更多信息,请参考 Rest API 发送HTTP请求,您也可以参考 API参考.

最后的建议是@Vincent所说的,如果您可以访问SDK代码,则也可以尝试使其适应UWP.这意味着您需要修改此SDK的源代码.

I am attempting to use Google Drive as a storage location in my UWP application. I started at the quickstart provided by Google. I copy the code into a blank UWP project, change some of the output code (Console.Writeline to a textbox.append method) and I try to build it. It fails to build and reports the error:

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

I am running Windows 10 and VS 2015 and I have installed the sdk through NuGet. The example code in the quickstart does work in a console application. It is the UWP application that is having issues.

For the UWP application, I put the quickstart code in a button click method. This was because the API actually has an async method for the uwp apps which is a bit different then the code given in the quickstart.

Includes:

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;

The Button Method:

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.");
            }
        }

The test code will not work once the app is compiled as it is missing the code to load the client secret. Since I have not been able to test the code, this is all I can provide.

There is another post that is semi-related except that the answer is just that it wont work and the post has been dead for 4 years. I also wanted to create a new post that tags the google team specifically (like the quickstart says to do).

My specific question is: Is there a work around to this issue or am I just doing this wrong?

解决方案

I agree with @Vincent, UWP apps use COM as a base and builds from there. Not all .Net API can be used in UWP apps, this SDK is based on .Net APIs, this is why your console app is OK, but your UWP app is down. For the differences between them, here is a great answer which explain this issue. But,

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

I just tried to search for this without any luck, but here is a suggestion, you can use JavaScript to make request to the Drive API. To do this, you can refer to JavaScript Quickstart. Then you can turn it to a web hosted UWP app, for more information, you can refer to Convert your web application to a Universal Windows Platform (UWP) app.

Another suggestion which can probably make the work easier is using Rest API to send HTTP requests, you can also refer to API Reference.

The final suggestion which is as @Vincent said, if you have access to the SDK code, you can also try to adapt it for UWP. It means you need to modify the source code of this SDK.

这篇关于在UWP App中使用Google Drive .NET API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆