【问题标题】:Receiving forbidden error message (403) when getting the list of beacons in Google Beacon Platform在 Google Beacon Platform 中获取信标列表时收到禁止的错误消息 (403)
【发布时间】:2016-11-20 15:12:29
【问题描述】:

我为我的 Google Proximity API 项目创建了一个服务帐户,并下载了密钥文件以在我的应用程序中使用它。但是当执行下面代码 sn-p 的最后一行时,我收到以下异常:

用户代码未处理 Google.GoogleApiException
HResult=-2146233088 消息=Google.Apis.Requests.RequestError 未经授权。 [403] 错误 [ 消息 [未经授权。] 位置 [ - ] 原因[禁止] 域[全局]]

这是应该在服务器机器(后端)上运行的代码:

X509Certificate2 certificate = new X509Certificate2(@"key.p12",
                "notasecret", X509KeyStorageFlags.Exportable);

            var credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer("MyServiceId@focal-baton-55555.iam.gserviceaccount.com")
               {
                   Scopes = new[] { "https://www.googleapis.com/auth/userlocation.beacon.registry" },
               }.FromCertificate(certificate));

            var beacon = new Google.Apis.Proximitybeacon.v1beta1.Data.Beacon();

            var beaconServices = new ProximitybeaconService(new BaseClientService.Initializer
            {
                ApplicationName = "My SampleProject",
                HttpClientInitializer = credential 
            });

            var result = await beaconServices.Beacons.List().ExecuteAsync();

我已经花了几个小时,但我根本无法得出任何结论。在这种情况下我缺少什么以及如何解决问题?

【问题讨论】:

  • 服务帐户需要预先授权我不确定您是否可以让其他用户访问该 API 中的数据,但如果您可以共享它,请使用服务帐户电子邮件地址并授予其访问权限

标签: c# google-api google-beacon-platform


【解决方案1】:

我通过在创建服务帐户时授予必要的权限解决了这个问题。那是我不幸错过的一步。然后我使用相同的 .p12 文件向 API 验证应用程序。

【讨论】:

  • 很高兴我能帮上忙。您介意为他人准确描述您是如何解决问题的吗?您如何授权服务帐号访问此 API。
猜你喜欢
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 2018-12-12
  • 2020-05-30
相关资源
最近更新 更多