kolsrud / qlik_rest_sdk

SDK for accessing the Qlik Sense REST APIs
MIT License
20 stars 4 forks source link

Error 403 #17

Closed victorvelasquez96 closed 2 years ago

victorvelasquez96 commented 2 years ago

Hi, sorry if i disturb again with a new problem. (thank you for the previous issue @kolsrud )

In my program i have a list of object that contains info about an app (id,name,streamid,streamname) called appData.

it begin with making a copy of the app then it moves the app in another stream.

my code is this -->

  public static void CopyApp (AppData appdata, string nameNewApp,string newStream)
        {
          //connection
            var senseServerUrl = domain;
            var uri = new Uri(senseServerUrl);
            var restClient = new RestClient(senseServerUrl);
            var networkCredentials = new NetworkCredential(user, password, userDomain);
            restClient.AsNtlmUserViaProxy(networkCredentials, certificateValidation:false);
          //restClient.ConnectAsQmc();

         //copying the app
          JObject temp  = restClient.Post<JObject>(string.Format("/qrs/app/{0}/copy?name={1}", appdata.Id, NameNewApp), "");

       //get new id from the app copied
         string idnewapp = temp.First.First.ToString(); 

       //publishing in new stream
         restClient.Put(string.Format("/qrs/app/{0}/publish?stream={1}", idnewapp , newStream), "");
}

When i use this method on one or two apps it works but when i select more (or sometimes only one) it gaves me this error when it arrives on the copying part.

error 403

I though that maybe was an app that doesn't have data or is a default app from Monitoring apps stream but it gives me error on apps that on a second run will move into the new stream.

kolsrud commented 2 years ago

Hi! As the message indicates, the first request you send to Qlik Sense needs to be a GET request if you use NTLM authentication. That's because the GET request will trigger a redirect flow that gets you authenticated. The RestClient class should take care of that automatically though, so I'm a little surprised to see you get that error. I'll try and see if I can reproduce it, but you can try to add a dummy GET request first as a work-around. Something like this for instance:

restClient.Get("/qrs/about");
restClient.Put(string.Format("/qrs/app/{0}/publish?stream={1}", idnewapp , newStream), "");
kolsrud commented 2 years ago

I'm afraid I haven't been able to reproduce the issue. Every call to the initial POST that performs the copy is preceded by a call to "GET /qrs/about" under the hood as expected. You'll see this if you enable a debug console, just add "var x = new RestClientDebugConsole(); to the start of you're program to do this. You should see this line just before the copy call:

var x = new RestClientDebugConsole();

But one thing I can recommend is to not create a new RestClient instance each time you copy. Instead, you can create one RestClient, set up the authentication, and pass it as an argument to CopyApp. That way you won't have to reauthenticate every time you call CopyApp. Like this:

public static void CopyApp (IRestClient restClient, AppData appdata, string nameNewApp,string newStream)
{
    //copying the app
    JObject temp  = restClient.Post<JObject>(string.Format("/qrs/app/{0}/copy?name={1}", appdata.Id, NameNewApp), "");

    //get new id from the app copied
    string idnewapp = temp.First.First.ToString(); 

    //publishing in new stream
    restClient.Put(string.Format("/qrs/app/{0}/publish?stream={1}", idnewapp , newStream), "");
}
kolsrud commented 2 years ago

I haven't heard anything for quite a while here. I'll close this ticket for now.