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

contacts

#1

Hi,

I’m trying to get Contact information by user token (hubspotutk). I created a console application to hit Contact API.
API URL: http://api.hubapi.com/contacts/v1/contact/utk/hubspotutk/profile?hapikey=********

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

        try
        {

            WebRequest request = WebRequest.Create("http://api.hubapi.com/contacts/v1/contact/utk/d464a15c6ec8b36b697420df612dc6f8/profile?hapikey=*************************");

            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);
            Console.ReadLine();



        }

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

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


#2

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.


#3

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