【问题标题】:Google Apis v3 in ASP.Net - "Unauthorized client or scope in request"ASP.Net 中的 Google APIs v3 - “未经授权的客户端或请求范围”
【发布时间】:2014-12-12 02:15:22
【问题描述】:

也许我根本没有得到“它”,“它”是完成这项工作所需的整体设置。

我有一个网站可以抓取其他网站的体育赛事。我想根据结果自动创建 Google 日历事件,因此我想在我的 GMail 帐户中的日历上授予我的 Web 应用程序读/写访问权限。

一个星期以来,我一直在努力解决这个问题,但我无法让它发挥作用,这让我作为开发人员的自尊心受到打击。

我“理解”的方式是我需要一个 Google API v3 服务帐户,因为我不需要特定用户的 API 密钥。还是我需要一个简单的 API 密钥(而不是 oAuth)?

无论如何,我使用的是服务帐户。

在我的 HomeController 中,我正在尝试获取日历,所以我知道这一切正常。

    public void Calendar()
    {
        string serviceAccountEmail = "...@developer.gserviceaccount.com";
        var certificate = new X509Certificate2(
            Server.MapPath("~") + @"\App_Data\key.p12",
            "notasecret",
            X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential =
            new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(serviceAccountEmail)
                    {
                        Scopes = new[]
                                {
                                    CalendarService.Scope.Calendar
                                },
                        User = "MY-GMAIL-EMAIL" // Is this correct?
                    }
                    .FromCertificate(certificate));

        BaseClientService.Initializer initializer = new BaseClientService.Initializer();
        initializer.HttpClientInitializer = credential;
        initializer.ApplicationName = "CALENDAR NAME"; // Correct?

        var service = new CalendarService(initializer);
        var list = service.CalendarList.List().Execute().Items; // Exception :-(
    }

我得到的错误:

An exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException' occurred in Google.Apis.dll but was not handled in user code

Additional information: Error:"unauthorized_client", Description:"Unauthorized client or scope in request.", Uri:""

所以我在 Google 日历中尝试了很多东西,比如将其公开,将服务帐户电子邮件添加为 READ/WRITE 用户。

我需要做什么来授权我的 Web 应用程序以便它可以代表我创建事件?

【问题讨论】:

  • 这个错误很可能是由于访问了日历API。将服务帐户电子邮件地址添加到您的日历中,这可能会使其访问您的日历。检查此链接developers.google.com/admin-sdk/directory/v1/guides/…。此外,如果您不想访问用户日历而不是服务帐户,则可以使用 Oauth

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


【解决方案1】:

我在类似的帖子中使用服务帐户完成了此操作。我更改了一些代码,并通过切换一些东西来列出我的日历。我也可以创建事件。我没有像您在初始化程序中那样添加用户,在应用程序名称下,它是开发控制台中应用程序的名称。确保为您的应用程序命名。确保您的服务帐号已与您的帐号共享。

我将您代码的列表部分稍微更改为我的代码,并取回了我的日历列表。

var list = service.CalendarList.List();
var listex = list.Execute();

Google API Calender v3 Event Insert via Service Account using Asp.Net MVC查看我的示例

【讨论】:

    猜你喜欢
    • 2015-07-06
    • 2015-02-01
    • 1970-01-01
    • 2017-02-22
    • 2016-10-13
    • 2015-10-11
    • 2022-09-19
    • 1970-01-01
    • 2020-04-30
    相关资源
    最近更新 更多