在某些请求上出现 (401) UnAuthorized 错误不是全部,但大多数

Getting (401) UnAuthorized error on some requests not all, but most

我认为这不是代码问题,但我们有一个包含数百个地址的列表需要处理。一些 return 数据,我们能够获得 Long 和 Lat,但大多数 return (401) UnAuthorized 错误。什么会导致这种情况发生?我们已经尝试传递 Host Headers 和其他所有内容,REST API 似乎在我们的开发环境中工作得更好,但在部署到我们的作业服务器时会抛出更多错误。对此问题的任何帮助将不胜感激。我们想了解为什么有些调用有效而其他调用无效,我们每次都传递相同的 apiKey,所以这真的很令人困惑。谢谢

这是使用 C# 的代码片段(进行中):

 //GET THE LATITUDE AND LONGITUDE BASED OFF THE PHYSICAL ADDRESS
String clientAddress = clientRow["home_address"].ToString() + ", " + clientRow["home_city"].ToString() + ", " + clientRow["home_state"].ToString() + ", " + clientRow["home_zip"].ToString();
Logger.Debug("CLIENT ADDRESS: {0}", clientAddress);
String geocoderUri = "https://geocode.search.hereapi.com/v1/geocode?q=" + clientAddress + "&apiKey=xxxxxxxxxxxxxxxxxxxxx"; //KEY REMOVED FOR POSTING ON STACK OVERFLOW
var syncClient = new WebClient();

var content = syncClient.DownloadString(geocoderUri);
//CREATE THE JSON SERIALIZER AND PARSE OUR RESPONSE
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AddressData));
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(content)))
{
    var addressData = (AddressData)serializer.ReadObject(ms);
    if (addressData.items.Count() > 0)
    {
        //INSERT THE LATITUDE AND LONGITUDE IN DB
        String sLat = addressData.items[0].position.lat.ToString();
        String sLong = addressData.items[0].position.lng.ToString();
        Logger.Debug("CLIENT GEOLOCATION - Longitude: {0} Latitude: {1}", sLong, sLat);
        insertLatLong(sLat, sLong, clientRow["clientID"].ToString(), 1);
    }
}

请您尝试使用 RestSharp 库进行休息api?

请看下面的示例代码。

var client = new RestClient("https://geocode.search.hereapi.com/v1/geocode?q="+ clientAddress);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer YOUR TOKEN");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);