访问路径Google.Apis.Auth被拒绝

时间:2017-03-23 10:45:11

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

我正在尝试实施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,
        });

1 个答案:

答案 0 :(得分:2)

默认情况下,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如何工作的教程FileDatastore demystified

相关问题