Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTTP login required for driver
#1
I want to write a driver that accesses a site tat requires the user to first login and then moves onto the pages with the detail required.

How do I do this with CML? How do I login first and then call a page after that.

I have asked if there was a way to pass the username and password in the URL but there is not.
Mykel Koblenz
Illawarra Smart Home
Reply
#2
What you do is you send the request. It will return you a 401 error. That means, you need to log in to do this. So you then build up an authentication string, which which you put into a header line and send back. Then you send it again.

In most cases, if you already have the information, you can skip the first round trip and just build and send the info the first time and it will take it. Presumably it's Basic or Digest type authentication? If so, there are helper methods on the HTTPClient class to build up the required values to send back in.
Dean Roddey
Explorans limites defectum
Reply
#3
BTW, you can set a user name and password on the HTTP client object and then enable auto-authorization and it will handle the 401 response for you. But, you can't get rid of the extra round trip if you do it that way. So it's ok for the occasional HTTP exchange, but no so much if doing ongoing access.
Dean Roddey
Explorans limites defectum
Reply
#4
I'm not sure if I am getting a 401 or not - its redirecting to a login page

This is the base URL https://myclear.clearnetworks.com.au/ Where I need to logon

Does this match what you are indicating?
Mykel Koblenz
Illawarra Smart Home
Reply
#5
In a real browser, in order to get the user name and password from you, it will typically have to response to 401 by going to a login screen, getting that info from you, then re-issuing the request. The browser might do that, or some javascript in the web site might handle it and take you to their own login screen.

When you are doing it yourself via code, you can handle the 401 any way you want, usually by just turning around and feeding in the authorization values based on info you have been configured with.

As I said, in most cases, you can pre-calculate the authorization value and just send it in up front, and avoid the whole extra round trip. That's best if the target server will accept it that way.
Dean Roddey
Explorans limites defectum
Reply
#6
I was going to point you at an existing driver that does this, but off all the ones I know use HTTP, nary a one of them does any authentication. But here's a simple example, where the auth info is being set up front. In this case it's Basic type authentication. For Digest, you'd build the digest type string instead, which is another method.

Code:
Locals=
        Card4   BodyBytes(0);
        Card4   StatusCode;
        URL     TarURL;
        String  RepText;
        String  ContType;
        String  AuthString;
        Card4   Index;
        Card4   Count;
    EndLocals;

    m_HTTP.CreateBasicAuthStr("someusername", "somepassword", AuthString);
    AuthString.Prepend("Basic ");

    TarURL.Set4
    (
        URLProtos.HTTPS
        , ""
        , ""
        , "atargetserver.com"
        , 0
        , "/info"
        , ""
        , ""
        , m_QParms
    );
    

    ContType := "application/json; charset=utf-8";
    m_KVPair.Set("Authorization", AuthString);
    m_InLines.AddObject(m_KVPair);

    // Indicate we have no outgoing body text to send
    StatusCode := m_HTTP.SendGET
    (
        TarURL
        , 4000
        , "MyAwesomeDriver"
        , "application/json; charset=utf-8"
        , RepText
        , m_OutLines
        , ContType
        , m_IOBuf
        , BodyBytes
        , False
        , m_InLines
        );
Dean Roddey
Explorans limites defectum
Reply
#7
Thanks - I plan to sit down this weekend to have a look at it so will digest it through the week and see what I come up with.
Mykel Koblenz
Illawarra Smart Home
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ClickPLC driver now failing after upgrade znelbok 2 440 09-21-2020, 10:48 PM
Last Post: znelbok
  Pentair driver tom 5 1,095 08-02-2020, 11:29 PM
Last Post: kblagron
  Get Field Value via HTTP znelbok 3 718 06-11-2020, 03:43 PM
Last Post: Dean Roddey
  HTTP Trigger and Parameters znelbok 1 493 06-11-2020, 03:41 PM
Last Post: Dean Roddey
  Marantz receiver driver (IP) dlmorgan999 6 1,106 05-15-2020, 03:32 PM
Last Post: dlmorgan999
  Variables Driver Client gReatAutomation 4 877 04-25-2020, 12:46 PM
Last Post: gReatAutomation
  Reconfig of Driver Causes Built In Triggers to Fire gReatAutomation 2 680 03-25-2020, 04:09 PM
Last Post: gReatAutomation
  Lutron RadioRA2 Driver and Lutron Visor Control gReatAutomation 29 8,068 03-19-2020, 01:03 PM
Last Post: gReatAutomation
  Timers Driver / Field Time Image / Seconds gReatAutomation 1 643 03-16-2020, 05:48 PM
Last Post: Dean Roddey
  Problem with Accesssing Variables Driver Client Interface kblagron 5 1,241 03-05-2020, 02:24 PM
Last Post: kblagron

Forum Jump:


Users browsing this thread: 1 Guest(s)