asp.net中的Google日历Api v3和OAuth2 ServiceAccountCredential

asp.net中的Google日历Api v3和OAuth2 ServiceAccountCredential,asp.net,oauth-2.0,google-calendar-api,Asp.net,Oauth 2.0,Google Calendar Api,我正在尝试使用服务帐户凭据连接到Googels API。我已经在谷歌开发者控制台中创建了一个服务帐户,我也在谷歌应用程序域帐户中做了所有设置,但它不起作用。当我尝试在我的谷歌日历中获取一个事件时,我只得到以下消息。Google.api.Auth.OAuth2.Responses.TokenResponseException:错误:“拒绝访问”描述:“请求的客户端未授权。”Uri:“ 拜托,我做错什么了 这是我的密码 using System; using System.Collections.G

我正在尝试使用服务帐户凭据连接到Googels API。我已经在谷歌开发者控制台中创建了一个服务帐户,我也在谷歌应用程序域帐户中做了所有设置,但它不起作用。当我尝试在我的谷歌日历中获取一个事件时,我只得到以下消息。Google.api.Auth.OAuth2.Responses.TokenResponseException:错误:“拒绝访问”描述:“请求的客户端未授权。”Uri:“

拜托,我做错什么了

这是我的密码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Apis.Services;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string serviceAccountEmail = ".....@developer.gserviceaccount.com";
        string keyFilePath = @"c:\nameName.p12";
        string  userEmail = "name.name@domain.com";
        string[] scopes = new string[] { CalendarService.Scope.Calendar, CalendarService.Scope.CalendarReadonly }; 

        var certificate = new X509Certificate2(keyFilePath, "secret", X--------509rageFlags&???.Exportable);
        var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
        {
            User = userEmail,
            Scopes = scopes
        }.FromCertificate(certificate));

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

        string eventid = "_60q30c1g60o0s46chk74oj0dhh64rj8ca16p138dpo74ojedhh8oq34dq4";

        Event myEvent = calService.Events.Get(userEmail, eventid).Execute();
        Literal1.Text = myEvent.Summary;

    }
}

如果您的应用程序访问用户数据,则需要授予您创建的服务帐户访问您要访问的Google Apps域的用户数据的权限

谷歌应用程序域的管理员必须执行以下步骤:转到谷歌应用程序域的管理控制台

从控件列表中选择安全性。如果看不到列出的安全性,请从页面底部的灰色栏中选择更多控件,然后从控件列表中选择安全性

从选项列表中选择高级设置

在“身份验证”部分中选择“管理第三方OAuth客户端访问”

在“客户端名称”字段中,输入服务帐户的客户端ID

在“一个或多个API作用域”字段中,输入应授予应用程序访问权限的作用域列表。例如,如果您需要对Google Drive API和Google Calendar API进行全域访问,请输入:

单击“授权”

另外,请检查您的代码中是否包含了应用程序试图访问的所有作用域


如果您仍然看到问题,请告诉我。

我发现了错误。你在SGC上是对的。我在代码中输入了CalendarService.Scope.Calendar和CalendarService.Scope.CalendarReadonly的作用域。 但在我们的谷歌应用程序域管理员控制台中,我刚刚进入了访问权限。我还必须补充一点。很明显,我真蠢


谢谢SGC

谢谢你的回答SGC,但我已经在我们的谷歌应用程序域帐户中完成了所有设置。我已经遵循了谷歌手册中的所有步骤,我也检查了,这是我们可以在范围中访问的日历。我的代码可能有问题吗?