Communications.makeWebRequest() returning 401 despite correct authorization token?

Hello,

I've recently started working on a watch app that makes a web request to a third party API. I'm stuck on authorization error - Response code: 401

I've included a copy of my code.

Please note I tried including the bearer token as is in String form in the params dictionary like so: "Authorization" => "Bearer secret_..."

This however resulted in the same response code of 401.

Perhaps the Authorization token needs to be added elsewhere?

Thanks in advance for the help!

function makeRequest() as Void {
        var url = "https://api-url-here/"
            + Rez.Strings.PageID +
            "/rest-of-api-url-here";
            
        var params = {
            "Authorization" => Rez.Strings.Authorization,
            "API-Version" => Rez.Strings.ApiVersion
        };

        var options = {
            :method => Communications.HTTP_REQUEST_METHOD_GET,
            :headers => {
                "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON},
            :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON // set response type
        };

        Communications.makeWebRequest(url, params, options, method(:onReceive));
    }

    function onReceive(responseCode as Number, data as Dictionary?) as Void {
        if (responseCode == 200) {
            System.println("Request Successful");                   // print success
        } else {
            System.println("Response: " + responseCode);            // print response code
        }

    }

  • Where you have something like

    Rez.Strings.PageID 

    you want to have WatchUi.loadResource(Rez.Strings.PageID )

    You aren't actually loading the strings

  • Thanks for your response! I was loading the resources just before I call makeRequest() in my BehaviorDelegate class. I tried replacing Rez.Strings.PageID with WatchUi.loadResource(Rez.Strings.PageID) (and the same for Authorization and API-Version) in the makeRequest() method itself, but I'm still returned the same response code of 401.

    I am certain the token and version fields I have included are correct as this request is functioning when I test on Postman.

  • The issue is that
    "Authorization" => Rez.Strings.Authorization,
    "API-Version" => Rez.Strings.ApiVersion
    are not params but are part of the headers.

    So try
    var params = null;

    var options = {
    :method => Communications.HTTP_REQUEST_METHOD_GET,
    :headers => {
    "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
    "Authorization" => Rez.Strings.Authorization,
    "API-Version" => Rez.Strings.ApiVersion},
    :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON // set response type
    };