0

I want to get access token for authentication. My post result like

POST https://staj-io-goldenilkay92-1.c9.io/api/v1/oauth/token 401 (Unauthorized)

but when I try to post with postman it works.

Server Side Headers

res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.header('Content-Type', 'application/json');

Angular Code

Service

function signIn(data) {

    var deferred = $q.defer();


    $http.post('https://staj-io-goldenilkay92-1.c9.io/api/v1/oauth/token', data,
        {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
    )
        .success(function (response, status, headers, config) {
            deferred.resolve(response);
        }).error(function () {
            deferred.reject("Failed to login");
        });

    return deferred.promise;

}

controller

  vm.loginData = {

    'client_id': 'client',
    'client_secret': 'client',
    'grant_type': 'password',
    'username': '',
    'password': ''
};


vm.login = function login() {

    loginService.signIn(vm.loginData).then(function (result) {
            vm.signInResult = result;

        },
        function (data) {

        });


}

Postman Result

POST https://staj-io-goldenilkay92-1.c9.io/api/v1/oauth/token 401 (Unauthorized)

2 Answers 2

1

Here is suggestions to solve your problem;

Use cors module (not required);

Server Side

I assume that your passport code working properly.

var cors= require('cors');

//init first.

app.options(cors({origin'*'}));  //Use your origins.
app.use(cors({origin'*'}));      //Use your origins.

Client Side

Just delete headers options

//...

$http.post('https://staj-io-goldenilkay92-1.c9.io/api/v1/oauth/token', data)
    .success(function (response, status, headers, config) {
        deferred.resolve(response);
    }).error(function () {
        deferred.reject("Failed to login");
    });
//...
Sign up to request clarification or add additional context in comments.

Comments

0

If one POST works and the other doesn't, then your angularjs $http request is making the request with the wrong parameters.

I'd suggest you to get an http analyser (like Fiddler) and compare the actual request done by Postman vs the request done by you angular app.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.