Digital Assistant and Mobile

Get Involved. Join the Conversation.

Topic

    Bala T
    401 Unauthorised error While calling MCS API in Ionic 3
    Topic posted July 20, 2017 by Bala TRed Ribbon: 250+ Points, last edited July 24, 2017 
    73 Views, 9 Comments
    Title:
    401 Unauthorised error While calling MCS API in Ionic 3
    Summary:
    401 Unauthorised error While calling MCS API in Ionic 3
    Content:

    Hi,

    I have developed an application for connecting cordova mcs client sdk in Ionic 3 but unfortunately I got unauthorised 401 error

    I have followed the steps given  below to call mcs API

    declare var mcs:any;

    var backend;

    var mcs_config = {

      "logLevel": mcs.LOG_LEVEL,

      "logHTTP": true,

      "mobileBackends": {

       "CUST_FW_SCREEN_MB1.0": {

       "default": true,

       "baseUrl": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",

       "applicationKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",

       "analytics": {

       "location": true

      },

       "authorization": {

       "basicAuth": {

       "backendId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",

       "anonymousToken": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

      }

      }

      }

      }

    }

    then I have intialised mcs like this

    constructor(public http: Http) {

       console.log('Hello SampleProvider Provider');

      }

    initializeMCS(){

       mcs.mobileBackendManager.platform = new mcs.CordovaPlatform();

       mcs.mobileBackendManager.setConfig(mcs_config);

       backend = mcs.mobileBackendManager.getMobileBackend("CUST_FW_SCREEN_MB1.0");

       console.log("Initialize mcs");

       if(backend != null){

      

       backend.setAuthenticationType("basicAuth");

      }

      }

    So then I called this function by passing user name and password

       login(username, password){

       this.initializeMCS();

      return backend.authorization.authenticate(username, password).then((data)=>{

       console.log(data);

      }).catch((error)=>{

       console.log("error");

      });

    I have passed the username and password to connect with the MCS

    logIn(){

       this.sample.login("XXXXXXXXXXXX", "XXXXXXXXX");

    }

    But I am getting unauthorised error 401

    Can you suggest any methods to resolve the issue

    Thank You

    Comment

     

    • Bala T

      Hi Thanks for the response

      a) The mobile backend name it self is "CUST_FW_SCREEN_MB1.0".

      b)The user name and password which are trying to authenticate is itself  MCS username and password.

      c) I have tried to debug the code

      mcs.PNG

      d)I have given the real values not XXXXX

    • Chris Muir

      Some things I notice:

      a) Your MCS Mobile Backend name is odd "CUST_FW_SCREEN_MB1.0".  Is "1.0" actually in the name of the Mobile Backend?  Typically we don't do this.

      b) Is the username+password you're trying to authenticate with included in the Realm mapped to the MCS Mobile Backend?  ie. does the user actually exist in MCS?

      c) As per my other post, I suggest you use the debugger to see what actual remote API call is being called when you get the 401, and post that information here too.

      d) Finally I assume within your mcs_config where you have all the XXXXXs, that's just you obfuscating the real values.

      CM.

    • Chris Muir

      Sorry, I don't understand your answer to "b".  What do you mean "is itself MCS username and password"?  Do you mean you're using the MCS user you use to login to the MCS cloud user interface?  That user does not have privileges on accessing the MCS APIs.  Only users that exist in the Mobile Backend Realm can access APIs.  You need to create a separate user in the Realm of the Mobile Backend to authenticate with from your app.

      On "c", unfortunately you've completely hidden the URL call to the external server.  This means I can't see which API is getting called.  Only hide the base URL of the URL so we can see what MCS resource is being called.

      CM.

    • Bala T

      Thanks for the quick response

      b)I am not using any username and password  just calling the API from mobile backend

      i just have used the method which have give below

      logIn(){

        this.sample.getdata("CUST_FW_SCREEN_API/CUST_FW_SCREEN_EP","POST",null).then(res =>{

         console.log(res);


        })

      }

      }

      here I am calling the API like this

      getdata(endpoint,method,body){

         mcs.mobileBackendManager.platform = new mcs.CordovaPlatform();

         mcs.mobileBackendManager.setConfig(mcs_config);

         backend = mcs.mobileBackendManager.getMobileBackend("CUST_FW_SCREEN_MB1.0");

         console.log(backend);

         console.log(endpoint);

         console.log(body);

      this.initializeMCS();

      return backend.CustomCode.invokeCustomCodeJSONRequest(endpoint , method , body).then(function(response){

         //The response parameter returns the status code and HTTP payload from the HTTP REST Call.

         console.log(response);

         return response;

         // Example: { statusCode: 200, data: {} }

         //Depends on the response format defined in the API.

        }).catch(function(response){

        //The response parameter returns the status code and HTTP payload, if available, or an error message, from the HTTP REST Call.

        console.log(response);

        return response;

      C)The debug code is showing error like this

      mcs 2.PNG

      Thanks

    • Chris Muir

      The error is being raised because the user you are logging into against MCS does not have privileges on the API you've built (/mobile/custom/CUST_FW_SCREEN_API/CUST_FW_SCREENEP).

      To test that the user does have privileges, login into MCS and use the test facility for the API in question.  When executing the API ensure to use a POST, and to use the same mobile backend, version, username and password.

      CM.

    • Chris Muir

      It's unclear when you say non using the SDK, do you mean inside the MCS test facility or from your own code.  I assume the latter.

      I suggest comparing the outgoing network calls to see what is different to see if you can work out why it isn't working.  In the end, the SDK makes a raw REST call just like your own code.  You have the ability to compare and work out what's going wrong.

      CM.

    • Bala T

      Thanks for the response

      Without using the mcs Sdk I am able to get the data from API but when I am using the MCS sdk I am not able to get the data

    • Bala T

      Thanks for the response ,

      I am using it  in the code which I have developed.

      When I am using custom API calls in my code I am able to get the data

      the url which i have given is

      public employeeUrl:string=this.baseUrl+"Employee_List_API/Emp_list";

      the method i have taken to call this API is

      getData(url, property) {

         let body = JSON.stringify(property);

         let headers = new Headers({ 'Content-Type': 'application/json', 'Authorization': 'my Authorisation Token', 'oracle-mobile-backend-id': 'my backend Id ' });

         let options = new RequestOptions({ headers: headers });

         return this.http.post(url, body, options)

        .map(res => res.json())

        .catch(this.handleError);

        }

      handleError(error) {

         console.error(error);

         return Observable.throw(error.json().error || 'Server error');

        }

      getEmployeeList() {

         let empUrl = this.singleton.employeeUrl;

         let body = {

         "P_PERSON_ID": XXXXXXXX

        }

         this.sample.getData(empUrl, body).subscribe(data => {

         console.log(data);

      Here I am able to get the data from the custom API

      When comming to the mcs Cordova Sdk  which I have been used to call the custom API

      This is the code I have developed

      declare var mcs:any;

      var backend;

      var mcs_config = {

        "logLevel": mcs.LOG_LEVEL,

        "logHTTP": true,

        "mobileBackends": {

         "CUST_FW_SCREEN_MB1.0": {

         "default": true,

         "baseUrl": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",

         "applicationKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",

         "analytics": {

         "location": true

        },

         "authorization": {

         "basicAuth": {

         "backendId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",

         "anonymousToken": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

        }

        }

        }

        }

      }

      getdata(endpoint,method,body){

         mcs.mobileBackendManager.platform = new mcs.CordovaPlatform();

         mcs.mobileBackendManager.setConfig(mcs_config);

         backend = mcs.mobileBackendManager.getMobileBackend("CUST_FW_SCREEN_MB1.0");

         console.log(backend);

         console.log(endpoint);

         console.log(body);

      this.initializeMCS();

      return backend.CustomCode.invokeCustomCodeJSONRequest(endpoint , method , body).then(function(response){

         //The response parameter returns the status code and HTTP payload from the HTTP REST Call.

         console.log(response);

         return response;

         // Example: { statusCode: 200, data: {} }

         //Depends on the response format defined in the API.

        }).catch(function(response){

        //The response parameter returns the status code and HTTP payload, if available, or an error message, from the HTTP REST Call.

        console.log(response);

        return response;

      logIn(){

        this.sample.getdata("CUST_FW_SCREEN_API/CUST_FW_SCREEN_EP","POST",null).then(res =>{

         console.log(res);

        })

      }

      }

      Can you please suggest me the ways to resolve the issuue

    • Chris Muir

      As mentioned:

      "I suggest comparing the outgoing network calls to see what is different to see if you can work out why it isn't working.  In the end, the SDK makes a raw REST call just like your own code.  You have the ability to compare and work out what's going wrong."

      As such use the debugger to compare the network calls for your code vs the SDK code, and see where the outgoing network call payloads differ.  This will identify why your code vs the SDK code is not working.

      CM.