3

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?

Community
  • 1
  • 1
Nibious
  • 47
  • 7

2 Answers2

1

The .Net flavor used to build Windows Store/UWP apps has less features than the full .Net framework. Unfortunately, the ExpandableObjectConverter object is not available for UWP applications.

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

If you have access to the SDK code, you can also try to adapt it for UWP.

Vincent
  • 3,656
  • 1
  • 23
  • 32
  • I couldn't find a UWP SDK. Thank you for helping find the solution though and confirming that I wasn't fully being an idiot :-P – Nibious Sep 29 '16 at 20:06
  • can you not use a .net standard 2.0 application and execute this code in it? it has more apis access – Emil Mar 19 '18 at 12:20
1

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.

Community
  • 1
  • 1
Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • Thanks for building on top of @Vincent's answer. I will try the Rest API route -- Guess it is better late than never to mess around with it. Thank you for taking your time to answer my question. – Nibious Sep 29 '16 at 20:05
  • i am tried to implement and it works follow the link and instructions only chnage your `client id` don't change the `redircturl` from project sample keep this same. [link](https://github.com/googlesamples/oauth-apps-for-windows) but problem is after acess token how to download,upload,delete files :( –  Aug 20 '19 at 07:26