对某些请求(不是全部,但大多数)上出现(401)未经授权的错误 [英] Getting (401) UnAuthorized error on some requests not all, but most

查看:44
本文介绍了对某些请求(不是全部,但大多数)上出现(401)未经授权的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不认为这是代码问题,但是我们有数百个地址要处理的列表.一些返回数据,我们可以得到经纬度,但是大多数返回(401)未经授权的错误.什么会导致这种情况发生?我们尝试传递主机头和其他所有内容,REST API在我们的开发环境中似乎更好地工作,但是在部署到我们的Job Server时会抛出更多错误.在这个问题上的任何帮助将不胜感激.我们想了解为什么有些调用有效而另一些无效,我们每次都传递相同的apiKey,因此确实令人困惑.谢谢

I don't think this is a code issue, but we have a list of hundreds of addresses to process. Some return data and we are able to get Long and Lat but most return (401) UnAuthorized errors. What would cause this to happen? We have tried passing Host Headers and everything else, the REST API seems to work better in our development environment but throws way more errors when deployed to our Job Server. Any help on this issue will be greatly appreciated. We would like to understand why some calls work and others don't, we pass the same apiKey each time so this is really confusing. Thanks

以下是使用c#的代码段(正在进行中):

Here is a code snippet using c# (Work in progress):

 //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库用于rest api吗?

Would you please try to use RestSharp lib for rest api?

请参见下面的示例代码.

Please see below sample code.

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);

这篇关于对某些请求(不是全部,但大多数)上出现(401)未经授权的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆