Digital Assistant and Mobile

Get Involved. Join the Conversation.

Topic

    Hanumanth Y
    Your Authentication type: basicAuth undefined,
    Topic posted March 1, 2017 by Hanumanth YRed Ribbon: 250+ Points, last edited March 24, 2017 
    85 Views, 22 Comments
    Title:
    Your Authentication type: basicAuth undefined,
    Summary:
    Your Authentication type: basicAuth undefined,
    Content:

    var mcs_config = {

      "logLevel": mcs.LOG_LEVEL.INFO,

      "logHTTP": true,

      "mobileBackends": {

        "TEST": {

          "default": true,

          "baseUrl": "https://development1-inalgarytmtrial.mobileenv.us2.oraclecloud.com:443",

          "applicationKey": "YOUR_BACKEND_APPLICATION_KEY",

            "authorization": {

            "basicAuth": {

              "backendId": "d838ab82-9755-4f54-bca1-38270afac134",

              "anonymousToken": "SU5BTEdBUllUTVRSSUFMX0RFVkVMT1BNRU5UMV9NT0JJTEVfQU5PTllNT1VTX0FQUElEOkFyZzJlanZfc3oxcW1h"

            }

          }

        }

      },

    };

      // initialize MCS mobile backend

        mcs.MobileBackendManager.setConfig(mcs_config);

        var mbe = mcs.MobileBackendManager.getMobileBackend('TEST');

        mbe.setAuthenticationType("basicAuth");

    var invokeCustomAPI = function(uri,method,payload) {

      var deferred = $q.defer();

       mbe.CustomCode.invokeCustomCodeJSONRequest(uri , method , payload

       , function(statusCode,data) {deferred.resolve(data)}

       , function(statusCode,data) {deferred.reject(statusCode,data)});        

       return deferred.promise;

    };

    var getHttpHeaders = function() {

        return mbe.getHttpHeaders();

    }

    var getCustomApiUrl = function(customUri) {

      return mbe.getCustomCodeUrl(customUri);   

    }

    return {

       authenticate:authenticate,

       authenticateAnonymous:authenticateAnonymous,

       logout:logout,

       invokeCustomAPI:invokeCustomAPI,

         getHttpHeaders:getHttpHeaders,

       getCustomApiUrl:getCustomApiUrl

    }

    In my controller i used

    $http({method:'GET'

                        ,url:mcsService.getCustomApiUrl("Test/HelloWorldPeople")

                        ,headers:mcsService.getHttpHeaders()})

                    .then(function(response){

                        $scope.departments = response.data;

                        console.log( $scope.departments);

                     })

                    .catch(function(err) {

                        console.log('Error calling endpoint /departments: '+err);

                    });

    When I used this code for connecting with the MCS mobile backend  I am getting basicAuth undefined and also the below error

    Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401.

    can any one please help me...

    Comment

    • Hanumanth Y

      I made the following changes in policies file

      *.*.Security_AllowOrigin=http\://localhost\:8100

      still i see the same error. Can you please suggest if i missed any step.

      Thank you!

    • Hanumanth Y

      Actually i done all the changes that you mentioned but I still get the same error.

      I am running the mobile application which is developed in ionic in my mobile I am getting an exception Access is denied.

      In MCS I enabled the basic Authorization.

      *.*.Security_AllowOrigin=http://localhost:8100  I mentioned this in policies document. Then it changed in the file as

      *.*.Security_AllowOrigin=http\://localhost\:8100

      still I am not able to connect.Please suggest me if any changes.

      Thank you!

    • Chris Muir

      There's a few things to comment on here, I'm not sure they will fix your problem but lets rule them out:

      a) In your mcs_config you haven't defined a value for your applicationKey.  In MCS under the MBE you should create a "Client" for your supported client platform (web, ios, android etc).  In creating the MBE client you will be given an applicationKey to copy into the mcs_config applicationKey element.

      b) Typically when you call this code:

      mcs.MobileBackendManager.setConfig(mcs_config);

          var mbe = mcs.MobileBackendManager.getMobileBackend('TEST');

          mbe.setAuthenticationType("basicAuth");

      you also need to tell the SDK what platform you're running on.  So if web:

      mcs.MobileBackendManager.platform = new mcs.BrowserPlatform();

      mcs.MobileBackendManager.setConfig(mcs_config);

          var mbe = mcs.MobileBackendManager.getMobileBackend('TEST');

          mbe.setAuthenticationType("basicAuth");

      or if Cordova:

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

      mcs.MobileBackendManager.setConfig(mcs_config);

          var mbe = mcs.MobileBackendManager.getMobileBackend('TEST');

          mbe.setAuthenticationType("basicAuth");

      c) For the following error message:

      Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401.

      This error can sometimes be a red herring, but typically means you need to setup MCS with a CORS exemption. Refer the following documentation section in the MCS Guide:

      https://docs.oracle.com/en/cloud/paas/mobile-cloud/mcsua/authentication-mcs.html#GUID-B5445574-1EEB-4431-9599-CA6215ACC9…

      Overall try fixing these 3 things then come back to us with a report.  I'm not 100% sure this is your problem, but as I said these stand out as problems so you need to fix these first.

      Thanks,

      CM.

    • Chris Muir

      Actually I asked you to do 3 things, you've indicated you've only done 1.  Please qualify if you've done all 3 that I suggested.

      One other thing to check on the MCS side, is have you enabled the Mobile Backend for basic authentication?

      Can you also please:

      a) Outline which line of code is raising the error with MCS

      b) Are you running a web or mobile app?

      Also did you add the escape forward slashes to the Security_AllowOrigin yourself?  MCS will add those for you, so your policy entry may now be incorrect.  Check that this is correct.


      CM.

    • Chris Muir

      Have you actually authenticated the user, by either calling?

      mbe.Authorization.authenticate(username, password, successCallback, failureCallback);

      ..or..

      mbe.Authorization.authenticateAnonymous(successCallback, failureCallback);

      Both are documented here:

      https://docs.oracle.com/en/cloud/paas/mobile-cloud/mcssc/Authorization.html

      CM.

    • Hanumanth Y

      I am able to login to mcs though application but I cant call the sample Api which is present in mobile backend.

      please suggest me for this

    • Chris Muir

      1) Is the REST API exposed through the MBE?

      2) Does the API have "Login Required" set to off, implying the API can be accessed anonymously?

      3) If set to true, have you defined a role for the API, and does the user who is accessing the API have that role?


      CM.

    • Hanumanth Y

      Yes I exposed REST API through MBE

      API Login Required is Off.

    • Chris Muir

      Then I don't know.

      Is there any other information you think that is relevant that might help me understand what's wrong?

      CM.

    • Hanumanth Y

      var invokeCustomAPI = function(uri,method,payload) {

        var deferred = $q.defer();

         mbe.CustomCode.invokeCustomCodeJSONRequest(uri , method , payload

         , function(statusCode,data) {deferred.resolve(data)}

         , function(statusCode,data) {deferred.reject(statusCode,data)});        

         return deferred.promise;

      };

      mcsService.authenticate($scope.loginData.username, $scope.loginData.password)

                      .then(function() {

                          alert('entered correctly')

                           mcsService.invokeCustomAPI("Test/HelloWorldPeople" , "GET" , null)

                                  .then (function(data) {

                                      $scope.departments = data;  

                                      console.log($scope.departments);      

                                      console.log("hii");

                                  })

                                  .catch(function(err) {

                                      console.log('Error calling endpoint /departments: '+err);

                              }); 

                           })

                      .catch(function(err) {

                          alert('Username or password is invalid')

                          }); 

      I am calling the sample HelloWorld API  from the mobile backend.My Backend name is Test.Is this the correct way to give the URL.I am getting 404 error now.Before I got 401 error and access error.

      I am able to connect to MCS but I can't call the Sample Hello World API.

      Please help me for this

    • Chris Muir

      "I am getting 404 error now.Before I got 401 error and access error."

      That's an important piece of information.  As such something you fixed has moved beyond authenticating the user which was previously failing, and now the user is being authenticated successfully and there is something wrong with the specific API call.

      Reasons for users not being able to call the API include:

      a) The API isn't exposed through the MBE

      b) The API is exposed through the MBE, but the user doesn't have the necessary privileges (roles) to access the API

      c) The base URL is configured wrong in your application

      d) The API URL is configured incorrectly in your application

      To help us identify what is going wrong, and to take JET out of the picture, have you tried calling your API from a remote client like POSTMAN?

      In terms of trying to work out if you have the URI/call to invokeCustomAPI correct, you should be running the application in the debugger and looking at the network layer to actually see what URL call goes out, which will be the combination of your base URL + the URI part in the call to invokeCustomAPI.  Learning how to use the debugger in a JET application is a key skill.  I suggest you review the debugging section in the following article:

      https://dzone.com/articles/installing-oracle-jet-for-hybrid-mobile-applicatio

      Regards,


      CM.

    • Hanumanth Y

      The Base url value is given same which is present in backend. But while calling the API in that, the API we got is not having port number in url. Why its not coming same as the API present.

      So please help me for this.

    • Chris Muir

      What port have you configured in your code and what port are you seeing in the debugger?


      If it's port 443 which is the SSL port, could it just be that the browser debugger is hiding the SSL port, but it is still hitting a https:// URL?

      Also have you tried testing this from POSTMAN?

      CM.

    • Hanumanth Y

      Yes, I tried this but in POSTMAN I am getting  error.

      But in MCS I am getting the JSON output.

      Please help me out for this

    • Hanumanth Y

      I connected the MCS Api using the Url directly in the http method, I got the output.

      But by using MCS sdk for cordova applications I am unable to connect to the Api in the backend that means I am getting 401 error.

      Please help me out for this.