Search code examples
ibm-mobilefirstworklight-adapters

IBM Worklight Adapters and OAuth -- header values in adapter


I'm currently working on an adapter call that takes an OAuth token in the Authorization header. The request works just fine in a FireFox RestClient AND an xhr get... but is failing from the Worklight adapter with an error saying the user needs to login. Which is the same error that the user would see if they didn't pass in the authorization header.

wl adapter:

function getFriends(accessToken) {
    var authtok = 'Bearer ' + accessToken;
    var input = {
        method : 'get',
        headers: {Authorization: authtok},
        path : '/connections/opensocial/rest/people/@me/@self'
    };  
    return WL.Server.invokeHttp(input);
}

Anyone else seeing an error like this? Is there a way to trace the worklight adapter invocation to inspect that the headers sent into the request are as expected?


Solution

  • Looks like your server was also expecting the hostname in the headers. Try changing your input to this:

    var input = {
            method : 'get',
            returnedContentType : 'json',
            path : '/connections/opensocial/rest/people/@me/@self',
            headers: {
                'Authorization': authtok,  
                Host: "HOSTNAME THAT IS USED IN THE .XML FILE"
            }
        };
    

    So if your adapter's xml file looks like this:

    <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
                <protocol>https</protocol>
                <domain>jeremy.server.whatever.com</domain>
                <port>443</port>    
                -->     
    </connectionPolicy>
    

    Then your headers should look like this:

    headers: {
        'Authorization': authtok,  
        Host: "jeremy.server.whatever.com"
    

    }