【发布时间】: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