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

    • Peter Flies

      What do you have set for your mobile backend configuration? Do you have the anonymous token set? Or are you trying to login as a MCS user? Sample below (with keys replaced with xxxxx):

      var mcs_config = {

              "logLevel": mcs.logLevelInfo,

              "mobileBackends": {

                  "mybackend": {

                      "default": true,

                      "baseUrl": "https://my.mobileenv.us2.oraclecloud.com:443",

                      "applicationKey": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",

                      "authorization": {

                          "basicAuth": {

                              "backendId": "xxxxxxxxxx-f13e-413f-92ca-d3ad204fec86",

                              "anonymousToken": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

                          },

                          "oAuth": {

                              "clientId": "xxxxxx-xxxxx-xx-xx-xx",

                              "clientSecret": "xxxxxxxxxxxxxxxxxxxxxxxxx",

                              "tokenEndpoint": "https://my.identity.us.oraclecloud.com/oam/oauth2/tokens"

                          }

                      }

                  }

              }

          };

    • Peter Flies

      Do you still get a 401 if you call the authenticateAnonymous method, and then invoke the custom service? If I call invokeCustomCodeJSONRequest without having called authenticateAnonymous I also get a 401. This snippet below works for me.

      mbe.Authorization.authenticateAnonymous(

                      function (response) {

                          console.log(response);

                          mbe.CustomCode.invokeCustomCodeJSONRequest("ProveIt/foo", "GET", null,

                                  function (response) {

                                      console.log(response);

                                  },

                                  function (statusCode) {

                                      console.log(statusCode);

                                  });

                      },

                      function (statusCode) {

                          console.log(statusCode);

                      }

              );

    • 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.invokeCustomAPI("Test/HelloWorldPeople/HelloWorldPeople" , "GET" , null)

                                  .then (function(data) {

                                      // $scope.departments = data;

                                      console.log(data);

                                      console.log("hii");

                                  })

                                  .catch(function(err) {

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

                              });

      I used this code. I am getting 401 error.Please help me out for this.

    • Peter Flies

      You have to call the authenticateAnonymous first, otherwise the Authorization header doesn't get added to your request. If you look in the network logs you should see the outgoing request and it will be missing the Auth header.  Once you call that, all subsequent requests should have the header. Can you give that a try (and watch the traffic to see if the auth header is on there).

    • Hanumanth Y

      var authenticateAnonymous = function() {

         var deferred = $q.defer();

          mbe.Authorization.authenticateAnonymous(

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

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

          return deferred.promise;

      };

      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.authenticateAnonymous()

                      .then(function() {

                          alert('entered correctly')

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

                                  .then (function(data) {

                                      // $scope.departments = data;

                                      console.log(data);

                                      console.log("hii");

                                  })

                                  .catch(function(err) {

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

                              });

                      })

      I used this code still I am getting 401 error.

      Please help me out for this.

    • Hanumanth Y

      Can you please check this and help me out for this?

    • Chris Muir

      From our perspective with apologies we've gone through just about every option we know where customers go wrong.  Without actually getting access to your MCS instance and code we're stuck to debug this more from the OTN forums.

      If you have a paid MCS account can you please lodge an SR.  If you are on a trial account, please reach out to your Oracle sales contact and see if they can provide assistance.

      CM.