Google Calendar API服务帐户错误401凭据无效 [英] Google Calendar API Service Account Error 401 Invalid credential

查看:201
本文介绍了Google Calendar API服务帐户错误401凭据无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Google日历服务帐户.

I'm trying to create a Google Calendar Service Account.

这是我的凭据:

这是我的服务帐户定义(已启用服务帐户的站点范围委派):

Here's my service account definition (Site Wide delegation for service account is enabled):

这是我的管理API客户端访问"页面:

Here's my "Manage API client access" page:

这是我的代码:

        string userName = "xxx@yyy.it";
        string[] Scopes = { CalendarService.Scope.Calendar, CalendarService.Scope.CalendarEvents };
        string ApplicationName = "Client per quickstart";
        ServiceAccountCredential credential = GoogleCredential.FromFile(@"path\credentialjson.json").CreateScoped(Scopes).CreateWithUser(userName).UnderlyingCredential as ServiceAccountCredential;

        // Create Google Calendar API service.
        var service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName
        });
        // THIS REQUEST FAILS!!  
        var y = service.CalendarList.List().Execute(); 

服务已正确创建,但所有请求均失败,并显示以下错误:

The service is created correctly but all requests fail with this error:

代码:401,消息:无效凭据",位置:授权",> LocationType:"header",原因:"authError",域:"global"

Code: 401, Message: "Invalid Credentials", Location: "Authorization", >LocationType: "header", Reason: "authError", Domain: "global"

我不明白授权标头中缺少什么.

I don't understand what I'm missing in the authorization header.

在创建服务时未完成的请求之前,我应该设置什么?谢谢!

What should I set before the request that was not done when creating the service? Thanks!

推荐答案

您的问题归因于您同时使用的范围,因为您正在设置 CalendarService.Scope.CalendarEvents ,这将是 CalendarService.Scope.Calendar 的优先级,因为Google API始终将最受限制的Scope作为优先级(在这种情况下, CalendarEvents ).

Your issue is due to the Scopes you are using at the same time, because you are setting CalendarService.Scope.CalendarEvents, this one will be the priority over CalendarService.Scope.Calendar, because the Google APIs always take as priority the most restricted Scope (in this case CalendarEvents).

您可以在 CalendarList中看到:列出端点作用域 CalendarService.Scope.CalendarEvents 不是您可以用来击中要击中端点的Scope.

And as you can see in the CalendarList: list endpoint scopes, the CalendarService.Scope.CalendarEvents is not a Scope you could use to hit the endpoint you wanted to hit.

因此,在您的代码中,您可以删除多余的Scope并像这样保留它:

So, in your code you can delete the extra Scope and leave it like this:

string[] Scopes = { CalendarService.Scope.Calendar };

这篇关于Google Calendar API服务帐户错误401凭据无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆