简体   繁体   中英

Google API .NET Throws UnauthorizedAccessException

I am attempting to use the Nuget package Google distributes to interface with their Google Calendar V3 API, detailed in the official tutorial here .

This is my calling code (all contained in a static wrapper class I wrote called GoogleCalendarAPI ):

// The file path the API credentials are stored in.
private static readonly string credentialsFilePath = "Assets/Config/credentials.json";

// The file path the authenticated token for API access is stored in.
private static readonly string authTokenFilePath = ApplicationData.Current.LocalFolder.Path + "\\token.json";

// The scopes within the API the app is accessing.
private static readonly string[] scopes = { CalendarService.Scope.CalendarReadonly };

// The name of the application to present to the API.
private static readonly string applicationName = "Minimalism Calendar";

// A cache of the authorized user credential for the Google Calendar API.
private static UserCredential credential = null;

public static async Task AuthorizeAsync()
{
    // This function is adapted from Google's tutorial: https://developers.google.com/calendar/quickstart/dotnet

    using (FileStream stream = new FileStream(
        GoogleCalendarAPI.credentialsFilePath, FileMode.Open, FileAccess.Read))
    {
        try
        {
            // This output line provides the correct (expected) output.
            System.Diagnostics.Debug.WriteLine("the path is: " + GoogleCalendarAPI.authTokenFilePath);

            GoogleCalendarAPI.credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                GoogleCalendarAPI.scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(GoogleCalendarAPI.authTokenFilePath, true));

            // This output line is never reached
            System.Diagnostics.Debug.WriteLine("B");
        }
        catch (Exception e)
        {
            // This error is NOT thrown.
            System.Diagnostics.Debug.WriteLine("ERROR: " + e.Message);
        }
    }
}

When I run the code, I get the following dialog box from Windows:

Windows 错误对话框

Based on the title of this dialog, and the fact that my input file paths appear to be valid and correct... I can only conclude that the error comes from something inside of Google's GoogleWebAuthorizationBroker.AuthorizeAsync() method?

I did notice that the ".NET" tutorial is specifically for a console app , and NOT a UWP app . The former of which has a lot fewer file access restrictions in place... so maybe what Google is doing in this method is valid for the former but not the latter?

Also important to note: the Visual Studio console does NOT display any errors nor error messages when this happens.

Any ideas?

UWP has limitations when accessing the file system. UWP apps could only access some specific location by default. Another thing is that UWP doesn't allow access files using Path . If you want to use a Path to get files, you need to enable the broadFileSystemAccess capability and only use Windows.Storage Namespace . For more detailed information, please check: File access permissions

In the code, I noticed that you have a parameter new FileDataStore(GoogleCalendarAPI.authTokenFilePath, true)) . This should be the reason for this behavior. The Google API is trying to access files using Path.

Currently, there are no other ways could bypass the UWP file limitations in UWP apps.

A possible solution for your scenario is that you could create a console app first, then implement the Google function you want in the console app. After that, you could package the console app and the UWP app together into a desktop bridge app. So when you want to call the Google function, you could just let the UWP app launch the console app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM