Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I am trying to connect to our QSR using the instructions outlined here - https://help.qlik.com/en-US/sense-developer/September2018/Subsystems/RepositoryServiceAPI/Content/Se...
I keep getting a 403 Forbidden error even though the .pfx file that I am using is generated using QMC itself.
The CRT and key file (both .pem formatted) used to generate the pfx file in the above step are working fine when used in Postman and I am getting the correct response (200 OK) as well as the entire json with the service details. Postman supports only CRT and key files but .NET supports pfx file only.
Any step I am missing ?
This is what my code looks like. Again, the exact same URL works great in postman but this code fails on the very first line of the try block .....
===============================================================================================
string Path = @"somefile.pfx";
string xrfkey = "123458989asqwsde";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://<hostname(omitted):4242/qrs/app? Xrfkey=123458989asqwsde");
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("X-Qlik-xrfkey", xrfkey);
request.Headers.Add("X-Qlik-User", @"UserDirectory=internal;UserId=sa_repository");
X509Certificate2 cert = new X509Certificate2(Path);
request.ClientCertificates.Add(cert);
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
Console.WriteLine(new StreamReader(stream).ReadToEnd());
}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
Hey Padmanabha,
I am by no means a C# programmer. But this code is running fine on my end:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string xrfkey = "123458989asqwsde";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://servername:4242/qrs/about?Xrfkey=123458989asqwsde");
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("X-Qlik-xrfkey", xrfkey);
request.Headers.Add("X-Qlik-User", @"UserDirectory=internal;UserId=sa_repository");
X509Certificate2 cert = new X509Certificate2("C:\\client.pfx", "password", X509KeyStorageFlags.MachineKeySet);
request.ClientCertificates.Add(cert);
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
Console.WriteLine(new StreamReader(stream).ReadToEnd());
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
}
}
Output:
C:\Users\levi\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2\bin\Debug>ConsoleApp2.exe
{"buildVersion":"23.3.1.0","buildDate":"9/20/2013 10:09:00 AM","databaseProvider":"Devart.Data.PostgreSql","nodeType":1,"sharedPersistence":true,"requiresBootstrap":false,"singleNodeOnly":false,"schemaPath":"About"}
This library provides some convenient functions for working with the REST API in C#:
https://github.com/kolsrud/qlik_rest_sdk
With that library you should be able to write like this:
var restClient = new RestClient(senseServerUrl);
var certs = RestClient.LoadCertificateFromDirectory(@"path/to/certs/folder", securePassword);
restClient.AsDirectConnection(certificateCollection: certs);
Console.WriteLine(restClient.Get("/qrs/app"));
Or if you have the certificates installed in the certificate store, then you could do like this:
var restClient = new RestClient(senseServerUrl);
var certs = RestClient.LoadCertificateFromStore();
restClient.AsDirectConnection(certificateCollection: certs);
Console.WriteLine(restClient.Get("/qrs/app"));
You can find a couple of examples on how to use that library here:
https://github.com/kolsrud/qlik_rest_sdk/tree/master/Qlik.Sense.RestClient/Examples
And that library is now available as a nuget package as well: