简体   繁体   English

谷歌 API .NET 抛出 UnauthorizedAccessException

[英]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 .我正在尝试使用 Google 分发的 Nuget package 与他们的 Google Calendar V3 API 接口,详细信息在此处的官方教程中。

This is my calling code (all contained in a static wrapper class I wrote called GoogleCalendarAPI ):这是我的调用代码(全部包含在 static 包装器 class 我写的称为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 得到以下对话框:

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?基于此对话框的标题,以及我的输入文件路径似乎有效且正确的事实......我只能得出结论,错误来自谷歌的GoogleWebAuthorizationBroker.AuthorizeAsync()方法内部的某些东西?

I did notice that the ".NET" tutorial is specifically for a console app , and NOT a UWP app .我确实注意到“.NET”教程专门针对控制台应用程序,而不是UWP 应用程序 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.同样重要的是要注意:发生这种情况时,Visual Studio 控制台不会显示任何错误或错误消息。

Any ideas?有任何想法吗?

UWP has limitations when accessing the file system. UWP 在访问文件系统时有限制。 UWP apps could only access some specific location by default. UWP 应用程序默认只能访问某些特定位置。 Another thing is that UWP doesn't allow access files using Path .另一件事是 UWP 不允许使用Path访问文件。 If you want to use a Path to get files, you need to enable the broadFileSystemAccess capability and only use Windows.Storage Namespace .如果要使用Path获取文件,则需要启用broadFileSystemAccess功能,并且仅使用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)) .在代码中,我注意到您有一个参数new FileDataStore(GoogleCalendarAPI.authTokenFilePath, true)) This should be the reason for this behavior.这应该是这种行为的原因。 The Google API is trying to access files using Path. Google API 正在尝试使用 Path 访问文件。

Currently, there are no other ways could bypass the UWP file limitations in UWP apps.目前,没有其他方法可以绕过 UWP 应用程序中的 UWP 文件限制。

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.您的方案的一个可能解决方案是您可以先创建一个控制台应用程序,然后在控制台应用程序中实现您想要的 Google function。 After that, you could package the console app and the UWP app together into a desktop bridge app.之后,您可以将 package 控制台应用程序和 UWP 应用程序一起整合成一个桌面桥应用程序。 So when you want to call the Google function, you could just let the UWP app launch the console app.因此,当您想调用 Google function 时,您可以让 UWP 应用程序启动控制台应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下载文件引发 UnauthorizedAccessException net core 2 - Downloading a file throws UnauthorizedAccessException net core 2 .NET Core 3.0 控制台应用程序无法读取文件,抛出 UnauthorizedAccessException - .NET Core 3.0 Console Application Cannot Read a File, throws UnauthorizedAccessException FileOpenPicker PickSingleFileAsync抛出UnauthorizedAccessException - FileOpenPicker PickSingleFileAsync throws UnauthorizedAccessException NamedPipeClientStream 在 Connect 上抛出 UnauthorizedAccessException - NamedPipeClientStream throws UnauthorizedAccessException on Connect FileOpenPicker引发UnauthorizedAccessException - FileOpenPicker throws UnauthorizedAccessException C# 注册表 SetValue 抛出 UnauthorizedAccessException - C# Registry SetValue throws UnauthorizedAccessException ContactManager.RequestStoreAsync()抛出System.UnauthorizedAccessException - ContactManager.RequestStoreAsync() throws System.UnauthorizedAccessException GetAccessControl抛出UnauthorizedAccessException访问SystemData目录 - GetAccessControl throws UnauthorizedAccessException accessing SystemData directory MemoryMappedFile.CreateFromFile始终抛出UnauthorizedAccessException - MemoryMappedFile.CreateFromFile always throws UnauthorizedAccessException NamedPipeClientStream 抛出 UnauthorizedAccessException:对路径的访问被拒绝 - NamedPipeClientStream throws UnauthorizedAccessException: Access to the path is denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM