简体   繁体   中英

Access $http.post response data

I creating small mobile application using cordova-ionic , which is authenticated by remote server which is integrated with oAuth2 .

First I tried to to get acccess_token then using that access_token I'm trying access other resources.

This is how I have tried.

function getToken(user){
    var username=user.userName;
    var password = user.password;
    var authResponse;
    var access_token;
    var headers = {
       'Authorization': 'Basic ' + TOKEN_ENDPOINT.basicAuth,
       //'Content-Type': 'application/x-www-form-urlencoded',
       'Access-Control-Allow-Origin':'*'
    }
    $http.defaults.headers.common.Authorization='Basic ' + TOKEN_ENDPOINT.basicAuth;
    $http({
        method: "POST",
        headers: headers,
        url: 'http://remote/oauth/',
    }).then(function (data, status) {
        if (status == '200') { 
            access_token=data.access_token;
            console.log("Auth.signin.success!")
            console.log(data);
        }
    })
    return access_token;
}

This is where I am trying to use access_token :

var goToProfile=function(user){
    var token = getToken(user);  
    if(token != null){
        $http({
            method:'POST',
            url:(API_ENDPOINT.url+'/profile',user),
            param:{access_token:token}
        }).then(function(result){
            console.log(result.data.success);
            console.log("Successfully signed in");
        })
    }else{
        console.log('dasd');
    }
}

So the problem is that the getToken method always returns undefined .

But in my console I can see the access_token . so how can I get the access_token .

Try this example and modify your code accordingly

$http({
    method: "post",
    url: "http://localhost:8081/employee-connect/oauth/token",
    data:  "username=1234567891&password=chaitanya&grant_type=password&scope=read write&client_secret=my-secret-token-to-change-in-production&client_id=employeeConnectapp",
    withCredentials: true,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
    .success(function(data) {
      window.localStorage.setItem("token_type", data.token_type);
      window.localStorage.setItem("token", data.access_token);


          $http({
            method: "get",
            url: "http://localhost:8081/employee-connect/api/articles",
            withCredentials: true,
            headers: {
              'Accept' : 'application/json, text/plain, */*',
              'Authorization' : ''+window.localStorage.getItem("token_type")+' '+window.localStorage.getItem("token")
            }
          })
              .success(function(data) {
                $scope.news = data;
              })
              .error(function(data, status) {
              });

    })
    .error(function(data, status) {
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM