Problem in getting Contact information by hitting HubSpot Contact API from server side code




I’m trying to get Contact information by user token (hubspotutk). I created a console application to hit Contact API.
API URL:********

If I hit this URL directly from Browser it works fine , but in application it sometime works fine but some time it throws an error ERROR 401: "The Remote server returned an error <401> : Unauthorized ". If it throw Error every time , it makes sense. But now I don’t know what’s wrong with code.

Application Code (C#) :

static void Main(string[] args)


            WebRequest request = WebRequest.Create("*************************");

            WebResponse response = request.GetResponse();

            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();

            var obj = JObject.Parse(responseFromServer);
            string fname = (obj.SelectToken("properties.lastname.value")).ToString();

            Console.WriteLine("{0}", fname);


        catch (Exception ex)
            Console.WriteLine("Error: " + ex.Message);

HubSpot API key is used for authorization.
Can anyone help with this.


Hi @Shergill

The URL itself looks ok. Do you have a way to log the body of the error response? There should be a message in there that gives more details for the problem.


Thanks @dadams for quick reply.

Here is the error message:

Message :The remote server returned an error: (401) Unauthorized.
StackTrace : at System.Net.HttpWebRequest.GetResponse()