jQWidgets Forums

jQuery UI Widgets Forums Plugins Data Adapter Token authentication

This topic contains 6 replies, has 3 voices, and was last updated by  mpotts 8 years, 10 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
  • Token authentication #80751

    gapfranco
    Participant

    I need to access data in a REST API source requiring token authentication.
    TO access the data I need to send a request header ‘Authentication’ ‘Bearer xxxx’, but it is not working. For example:

    var source =
        {
            dataType: "json",
            dataFields: [
                { name: 'id', type: 'number' },
                { name: 'name', type: 'string' },
                { name: 'upid', type: 'number' }
            ],
            hierarchy:
            {
                keyDataField: { name: 'id' },
                parentDataField: { name: 'upid' }
            },
            id: 'id',
            url: "http://localhost:9000/ird/v1/areas"
        };
        var dataAdapter = new $.jqx.dataAdapter(source, {
            beforeSend: function (jqXHR, settings) {
                jqXHR.setRequestHeader('Authorization', 'Bearer xxxxxxx');
            }
        });

    The beforeSend function is being called but the header is not being set. Any clues?

    Token authentication #80773

    Peter Stoev
    Keymaster

    Hi gapfranco,

    No, we do not know why the header is not
    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com

    Token authentication #80782

    gapfranco
    Participant

    Hi Peter,

    But it should, right? I mean, the use of beforeSend is correct? Any ideas on how to debug it? There is no error messages.
    Or must we assume that the request header cannot be set? That would be a show stopper on our use of jqwidgets.

    Best regards,

    Token authentication #80790

    Peter Stoev
    Keymaster

    Hi gapfranco,

    Sorry to learn that. This code seems like a “hack” to me. According to me, the contentType should be used instead.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com

    Token authentication #80796

    gapfranco
    Participant

    Hi Peter,

    A “hack”? I just followed the documentation:

    beforeSend(jqXHR, settings): A pre-request callback function that can be used to modify the jqXHR

    There is no exemples on usage.

    How can I set a request authorization header using the contentType?
    And how to use this library to access the Twitter API, for exemple, that also requires an Authorization header? For exemple:

    POST /1/statuses/update.json?include_entities=true HTTP/1.1
    Accept: */*
    Connection: close
    User-Agent: OAuth gem v0.4.4
    Content-Type: application/x-www-form-urlencoded
    Authorization: OAuth oauth_consumer_key="xvz1evFS4wEEPTGEFPHBog", ...
    Content-Length: 76
    Host: api.twitter.com

    There is the Content-Type AND there is the Authorization headers. The question is: How to set that Authorization header?

    Best regards,

    Token authentication #80816

    Peter Stoev
    Keymaster

    Hi gapfranco,

    I believe xhrFields should be used in this case, too. We don’t have such field so your choice is to use basic jQuery Ajax call.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com

    Token authentication #86035

    mpotts
    Participant

    Hi All,

    This approach (beforesend: … setRequestHeader …) works for me and includes the custom header I specify in the HTTP request.

    However the inclusion of a custom header may cause the browser to precede the HTTP request with a pre-flight “OPTIONS” request, which must be responded to with a “200 Ok” including an appropriate “Access-Control-Allow-Headers: …” before the actual HTTP request will be sent.

    “Authorization” is a standard HTTP header for basic authentication and should probably not be used for a fully custom header.

    Hope this helps.

    Mark

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.