.Net (C#)

Note: The following .Net C# code sample is compatible with Visual Studio 2017 and above. Download and unzip ‘DotNetSdk’ to find the solution files inside PreciselyAPIsCSharp_DirectAPI_Access folder that are required to run this code in respective Visual Studio version. To run this code using Visual Studio 2017, refer CodesamplesCSharp.sln solution file.
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;

namespace IdentityProfilesSample
{
    class IdentityProfilesSample
    {
        //FIXME Assign your Client Id here
        private static String API_Key = "";

        //FIXME Assign your Secret here
        private static String SECRET = "";

        private static String AUTH_HEADER = "Authorization";
        private static String OAUTH2_TOKEN_URL = "https://api.precisely.com/oauth/token";


        private static String accessToken;

        private static String BEARER = "Bearer ";

        private static String API_FRAGMENT = "identityprofiles/v1/identity/";

        private static String PRECISELY_API_URL = "https://api.precisely.com/" + API_FRAGMENT;

        static void Main(string[] args)
        {
            //Acquires OAuth2 token
            acquireAuthToken();

            getIdentityByAddress(true, "1 Global View, troy, NY", null, null, null, null);

            getIdentityByAddress(false, "1 Global View, troy, NY", null, null, null, null);

            getIdentityByEmailAddress(true, "ken@fullcontact.com", "low", null, null);

            getIdentityByEmailAddress(false, "ken@fullcontact.com", "max", null, null);

            getIdentityByTwitter(true, "nelrahcaz", "high", null, null);

            getIdentityByTwitter(false, "nelrahcaz", "high", null, null);
        }

        private static void getIdentityByTwitter(bool responseTypeIsXml, string twitter, string confidence, string theme, string filter)
        {
            String apiUrl = "bytwitter?twitter=" + twitter;

            if (confidence != null || confidence != "")
            {
                apiUrl += "&confidence=" + confidence;
            }
            if (theme != null && theme != "")
            {
                apiUrl += "&theme=" + theme;
            }

            if (filter != null && filter != "")
            {
                apiUrl += "&filter=" + filter;
            }

            processRequest(responseTypeIsXml, apiUrl);

        }

        private static void getIdentityByEmailAddress(bool responseTypeIsXml, String emailAddress, String confidence, String theme, String filter)
        {
            String apiUrl = "byemail?email=" + emailAddress;

            if (theme != null && theme != "")
            {
                apiUrl += "&theme=" + theme;
            }

            if (filter != null && filter != "")
            {
                apiUrl += "&filter=" + filter;
            }

            apiUrl = parameterizeGeoIdentity(confidence, null, apiUrl);

            processRequest(responseTypeIsXml, apiUrl);
        }


        private static void getIdentityByAddress(bool responseTypeIsXml, String address, String confidence, String maxCandidates, String theme, String filter)
        {
            string apiUrl = "byaddress?address=" + address;

            if (theme != null && theme != "")
            {
                apiUrl += "&theme=" + theme;
            }

            if (filter != null && filter != "")
            {
                apiUrl += "&filter=" + filter;
            }

            apiUrl = parameterizeGeoIdentity(confidence, maxCandidates,
                    apiUrl);

            processRequest(responseTypeIsXml, apiUrl);
        }
        private static void processRequest(Boolean responseTypeIsXml, String apiUrl)
        {

            string acceptHeader = String.Empty;
            if (responseTypeIsXml)
            {
                acceptHeader = "application/xml";
            }
            else
            {
                acceptHeader = "application/json";
            }

            string endPoint = PRECISELY_API_URL + apiUrl;
            var response = MakeRequest(acceptHeader, endPoint, accessToken);
            Console.Write(response);
            Console.ReadKey();
        }

        private static string MakeRequest(string acceptHttpHeader, string endPoint, String accesstoken, string httpMethod = "GET", string postData = null)
        {
            var request = (HttpWebRequest)WebRequest.Create(endPoint);
            var responseValue = string.Empty;
            request.Method = httpMethod;
            request.ContentLength = 0;
            request.Accept = acceptHttpHeader;


            if (!string.IsNullOrEmpty(accesstoken))
            {
                request.Headers.Add(AUTH_HEADER, accesstoken);
            }

            try
            {

                if (!string.IsNullOrEmpty(postData) && httpMethod == "POST")
                {
                    var encoding = new UTF8Encoding();
                    var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(postData);
                    request.ContentLength = bytes.Length;

                    using (var writeStream = request.GetRequestStream())
                    {
                        writeStream.Write(bytes, 0, bytes.Length);
                    }
                }



                using (var response = (HttpWebResponse)request.GetResponse())
                {


                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                        throw new ApplicationException(message);
                    }

                    // grab the response
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                            using (var reader = new StreamReader(responseStream))
                            {
                                responseValue = reader.ReadToEnd();
                            }
                    }


                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }
            return responseValue;
        }

        private static String parameterizeGeoIdentity(String confidence,
        String maxCandidates, String apiUrl)
        {
            if (confidence != null)
            {
                apiUrl += "&confidence=" + confidence;
            }
            if (maxCandidates != null)
            {
                apiUrl += "&maxCandidates=" + maxCandidates;
            }

            return apiUrl;
        }

        /**
        * Acquires OAuth2 token for accessing LocationIntelligence APIs
        */
        private static void acquireAuthToken()
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            AdmAccessToken token = null;
            var request = (HttpWebRequest)WebRequest.Create(OAUTH2_TOKEN_URL);
            String PostData = string.Format("grant_type=client_credentials");
            String ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = ContentType;

            var encoding = new UTF8Encoding();
            var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
            request.ContentLength = bytes.Length;

            String AuthorizationHeaderValue = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(API_Key + ":" + SECRET));

            WebHeaderCollection headers = new WebHeaderCollection();

            headers.Add(HttpRequestHeader.Authorization, AuthorizationHeaderValue);
            request.Headers = headers;

            try
            {

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }





                using (WebResponse webResponse = request.GetResponse())
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken));
                    //Get deserialized object from JSON stream
                    token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
                    accessToken = BEARER + token.access_token;
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }

        }

        /**
       Class representing OAuth response Object
        */
        [DataContract]
        class AdmAccessToken
        {
            [DataMember]
            public string access_token { get; set; }
            [DataMember]
            public string token_type { get; set; }
            [DataMember]
            public string expires_in { get; set; }
            [DataMember]
            public string scope { get; set; }
        }
    }
}