Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

How to provide access to a user with QRS API using RestSharp or HttpClient method?

I am trying to provide access to users with QRS API call in ASP.Net Web Application using C# I am using Client provided certificates to authorize the request. How to do it. Can anyone explain?
My code goes something like this.

Program Body:

                X509Certificate2 _certificate;

                _certificate = new X509Certificate2(@"Qlik Certificate path");

                ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

                var client = new RestClient("https://server_name.domain_name.net");

                //client.Authenticator = new HttpBasicAuthenticator(username, password);

                var request = new RestRequest("/qrs/user", Method.GET);                           //QRS endpoint for users

                request.AddHeader("Xrfkey", "12345678qwertyui");

                request.AddHeader("Content-Type", "application/json");

                X509Certificate2Collection certs = new X509Certificate2Collection();

                certs.Add(_certificate);

                client.ClientCertificates = certs;

                 var asyncHandle = client.ExecuteAsync<User>(request, response =>

                 { Console.WriteLine(response.Data); });

for response capturing:

public class User

        {

            public int id { get; set; }

            public string userId { get; set; }

            public string userDirectory { get; set; }

            public string name { get; set; }

            public string privileges { get; set; }

        }

I have also tried this for showing result:

client.ExecuteAsync(request, response =>

{ Console.WriteLine(response.Content); });

I am calling this function on Button click event.

1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

I did try with small changes and it worked for me... Thanks for replying. Below is the dummy of what I did there exactly.

X509Certificate2 SenseCert = new X509Certificate2(@"Qlik Certificate path");

                ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

                //Create the HTTP Request and add required headers and content in xrfkey

                string xrfkey = "0123456789abcdef";

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://server_name.domain_name.net");

                request.Method = "GET";

                request.Accept = "application/json";

                request.Headers.Add("X-Qlik-xrfkey", xrfkey);

                // Add the certificate to the request and provide the user to execute as

                request.ClientCertificates.Add(SenseCert);

                request.Headers.Add("X-Qlik-User", @"UserDirectory=UserDirectoryName;UserId=UserID");

                // make the web request and return the content

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Stream stream = response.GetResponseStream();

                Response.Text = new StreamReader(stream).ReadToEnd();

                string jsonString = Response.Text.ToString();

                var _Data = JsonConvert.DeserializeObject<List<User>>(jsonString);

                foreach (User objUser in _Data)

                {

                    Response.Text = objUser.userId + " " + objUser.id + " " + objUser.name + " " + objUser.userDirectory + " " + objUser.schemaPath + " " + objUser.privileges;

                }

Response read by this:

public new class User

        {

            public string id { get; set; }

            public string userId { get; set; }

            public string userDirectory { get; set; }

            public string name { get; set; }

            public string privileges { get; set; }

        }

View solution in original post

4 Replies
Øystein_Kolsrud
Employee
Employee

Is it the response deserialization that is the problem? Or is it that you don't get a valid response?

I've added this project to branch for doing rest API calls in C#. It may be of use to you, or at least give an en example of how it can be done.

Anonymous
Not applicable
Author

I did try with small changes and it worked for me... Thanks for replying. Below is the dummy of what I did there exactly.

X509Certificate2 SenseCert = new X509Certificate2(@"Qlik Certificate path");

                ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

                //Create the HTTP Request and add required headers and content in xrfkey

                string xrfkey = "0123456789abcdef";

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://server_name.domain_name.net");

                request.Method = "GET";

                request.Accept = "application/json";

                request.Headers.Add("X-Qlik-xrfkey", xrfkey);

                // Add the certificate to the request and provide the user to execute as

                request.ClientCertificates.Add(SenseCert);

                request.Headers.Add("X-Qlik-User", @"UserDirectory=UserDirectoryName;UserId=UserID");

                // make the web request and return the content

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Stream stream = response.GetResponseStream();

                Response.Text = new StreamReader(stream).ReadToEnd();

                string jsonString = Response.Text.ToString();

                var _Data = JsonConvert.DeserializeObject<List<User>>(jsonString);

                foreach (User objUser in _Data)

                {

                    Response.Text = objUser.userId + " " + objUser.id + " " + objUser.name + " " + objUser.userDirectory + " " + objUser.schemaPath + " " + objUser.privileges;

                }

Response read by this:

public new class User

        {

            public string id { get; set; }

            public string userId { get; set; }

            public string userDirectory { get; set; }

            public string name { get; set; }

            public string privileges { get; set; }

        }

Øystein_Kolsrud
Employee
Employee

Just realized I never included the link... Here it is: Qlik Branch

Anonymous
Not applicable
Author

Looks Good. Thanks!