c# - 尝试创建新事件时出现身份验证范围不足错误

标签 c# google-api google-oauth google-calendar-api google-api-dotnet-client

我想使用 C# 在 Google 日历中创建事件。我正处于编程的初级阶段,但我想尝试解决这个问题。

代码:

private void GoogleAPI_Add_events()
    {
        UserCredential credential;

        using (var stream =
            new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
        {
            // The file token.json stores the user's access and refresh tokens, and is created
            // automatically when the authorization flow completes for the first time.
            string credPath = "token.json";
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }

        // Create Google Calendar API service.
        var service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define parameters of request.
        EventsResource.ListRequest request = service.Events.List("primary");
        request.TimeMin = DateTime.Now;
        request.ShowDeleted = false;
        request.SingleEvents = true;
        request.MaxResults = 8;
        request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
        var ev = new Event();
        EventDateTime start = new EventDateTime();
        start.DateTime = new DateTime(2021, 3, 11, 10, 0, 0);

        EventDateTime end = new EventDateTime();
        end.DateTime = new DateTime(2021, 3, 15, 10, 0, 0);
        ev.Start = start;
        ev.End = end;
        ev.Summary = "New Event";
        ev.Description = "Description...";

        var calendarId = "primary";
        Event recurringEvent = service.Events.Insert(ev, calendarId).Execute();
        MessageBox.Show("New evento creato");
    }

我收到此错误:

Google.GoogleApiException: 'Google.Apis.Requests.RequestError Request had insufficient authentication scopes. [403] Errors [

最佳答案

Request had insufficient authentication scopes.

基本上意味着您当前对您的应用程序进行身份验证的用户尚未授予您执行您尝试执行的操作所需的权限。

您正在尝试使用 Events.insert方法需要以下范围之一。

enter image description here

您尚未发布您要作为范围发送的内容,但我猜它不是其中之一。

注意:

请记住,当您更改范围时,您要么需要更改“用户”文本,要么删除 credPath 中存储的用户的凭据文件,否则它不会为您的应用程序请求新的授权。

在下面的代码中,术语“用户”表示您正在登录的用户,如果您已经使用该用户登录,那么该用户的凭据将由 FileDataStore 存储在 credsPath 目录中,您应该有一个目录称为 token.json。

将“user”更改为其他字符串“user1”,或者进入该目录并删除该文件。

 credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;

关于c# - 尝试创建新事件时出现身份验证范围不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65992963/

相关文章:

api - 如何获取博客 API 帖子/搜索的下一页结果?

firebase - react-native google api 限制

google-api - 如何在使用 Google API JS 客户端时获取刷新 token

c# - Google ML-Engine Predict from C# 身份验证问题

c# - Visual Studio 重构重命名导致 XAML 文件中无法解释的属性全局替换

c# - 允许全局地址列表 (GAL) 集成的 ASP.NET 控件

c# - 如何列出特定文件夹中的文件夹和文件

.net - Razor 页面中的 Google 身份验证(Blazor 服务器项目)

c# - WinForms:从其变量名中获取某个按钮

c# - 对 ASP.NET 论坛控件的建议?