【问题标题】:Access to the path Google.Apis.Auth is denied访问路径 Google.Apis.Auth 被拒绝
【发布时间】:2017-03-23 10:45:10
【问题描述】:

我正在尝试实现 google 日历事件同步功能,以将事件从我的本地应用程序安排到 google 日历。它在本地系统中工作正常,但每当我将它部署在服务器上时,它都会引发以下错误。

System.UnauthorizedAccessException:对路径“Google.Apis.Auth”的访问被拒绝

下面是我使用的代码。

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets
        {
            ClientId = "xxx-9khdjsqifkj2amsji2jce37p8lfn0166.apps.googleusercontent.com",
            ClientSecret = "ZdsrCa-uwG3GmpVLTfYDli-S",
        },
        new[] { CalendarService.Scope.Calendar },
        "user",
        CancellationToken.None).Result;

        // Create the service.
        var service = new CalendarService(new BaseClientService.Initializer
        {
            HttpClientInitializer = credential,
            ApplicationName = Summary,
        });

【问题讨论】:

  • 你应该在你的项目上重新生成你已经与世界分享你的客户 ID 的秘密,这是你应该自己保留的东西。

标签: c# asp.net-mvc google-api google-calendar-api google-api-dotnet-client


【解决方案1】:

默认情况下,Google .Net 客户端库使用 FileDatastore() 文件数据存储,默认情况下将凭据存储在 %appData% 中。当您在任何用户下运行应用程序时,您将无法访问 %appData%

string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets
        {
            ClientId = "xxx4671204-9khdjsqifkj2amsji2jce37p8lfn0166.apps.googleusercontent.com",
            ClientSecret = "ZdsrCa-uwG3GmpVLTfYDli-S",
        },
        new[] { CalendarService.Scope.Calendar },
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)).Result;

确保 credPath 是您有权写入的某个位置。我在 GitHub 上有一个Google calendar Oauth2 的示例,我有一个关于文件数据存储如何工作的教程FileDatastore demystified

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-16
    • 2014-06-17
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多