由于错误403(禁止),Google . net API失败

本文关键字:Google net API 失败 于错误 错误 禁止 | 更新日期: 2023-09-27 18:18:26

下面的代码片段应该返回信标列表。当有谷歌API控制台生成API密钥时,我已将我的公共IP地址列入白名单,并与API密钥相关联。当代码调用ExecuteAsync()方法时,我一直收到错误码403(禁止)的异常。我可能做错了什么?如何减轻这个问题?

public async void TestApiKey()
{
    var apikey = "739479874ABCDEFGH123456"; //it's not the real key I'm using
    var beaconServices = new ProximitybeaconService(new Google.Apis.Services.BaseClientService.Initializer
    {
        ApplicationName = "My Project",
        ApiKey = apikey
    });
    var result = await beaconServices.Beacons.List().ExecuteAsync();
    // Display the results.
    if (result.Beacons != null)
    {
        foreach (var api in result.Beacons)
        {
            Console.WriteLine(api.BeaconName + " - " + api.Status);
        }
    }
}

由于错误403(禁止),Google . net API失败

您正在使用公共API密钥。公共API密钥只适用于公共数据。

灯塔。使用OAuth访问令牌进行身份验证已登录的用户,具有查看器,是所有者或可以编辑权限。

需要以下OAuth作用域:
•https://www.googleapis.com/auth/userlocation.beacon.registry

您试图访问的方法正在访问私有用户数据。您需要通过身份验证才能使用它。切换到Oauth2认证方式。将其设置为public可能行不通,因为据我所知,您无法为公共api密钥提供作用域。