1

I am trying to use Angular to authenticate against an authorization endpoint that I know works using Postman.

<script type="text/javascript">
    var tokenGeneratorApp = angular.module('myApp', []);

    myApp.controller('AuthenticationController', function ($scope, $http) {
        var ac = this;
        ac.authorizationToken = null;

        ac.getAuthorizationToken = function () {
            $http({
                method : 'POST',
                url: 'https://api.myserver.net/oauth/token',
                data: {
                    grant_type: 'password',
                    username: 'theUserName', 
                    password: 'thePassword'
                },
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(_authenticationSuccess, _authenticationError);
        };

        // Private methods to handle promise results

        function _authenticationSuccess(response) {
            ac.authorizationToken = response.data;
            ac.resultsDisplay = ac.authorizationToken;
        };

        function _authenticationError(response) {
            ac.resultsDisplay = 'An error occured: ' + response.data;
        };
    });
</script>

When I call getAuthorizationToken()I get an Http 400 back. When I look into the response.data object there is an error saying error:"unsupported_grant_type". This is confusing to me because in the Postman client I specify that the grant_type as password and all works as expected.

I must be doing something wrong in the Angular code.

2 Answers 2

1

Had a very similar problem recently. Try removing the 'headers' and insert 'dataType' instead, as follows:

        $http({
            method : 'POST',
            url: 'https://api.myserver.net/oauth/token',
            dataType: "json",
            data: {
                grant_type: 'password',
                username: 'theUserName', 
                password: 'thePassword'
            }

EDIT

        $http({
            method : 'POST',
            url: 'https://api.myserver.net/oauth/token',
            data: {
                "username=" + theUserName + "&password=" +   
                thePassword + "&grant_type=thePassword"
            },
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the reply. I made the change you indicated but I get the same error.
Can you try instead changing the header to 'Content-type': 'application/x-www-form-urlencoded'
Can I just verify you've tried the following, without the 'dataType' included instead: $http({ method : 'POST', url: 'api.myserver.net/oauth/token', data: { grant_type: 'password', username: 'theUserName', password: 'thePassword' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
Yes, everything is just as my example in the original question except the headers are now 'Content-Type': 'application/x-www-form-urlencoded'
The only other solution I can understand may solve this issue is the format of the 'password'. The answer to this link (social.msdn.microsoft.com/Forums/sqlserver/en-US/…) outlines a structure of how the password should look, which is the only solution I can think of
|
0

//resolving => error:"unsupported_grant_type"

vm.BtnLogin = function () {

    $scope.txtUsernamee;
    $scope.txtPasswordd;

    var client_credentials =  $scope.txtUsernamee +   $scope.txtPasswordd;
    var auth = 'username=' + $scope.txtUsernamee + '&' + 'password=' + $scope.txtPasswordd + '&grant_type=password';

    $http({
        method: "POST",
        url: '/token',
        contentType: 'application/json',
        data: auth

    }).then(function success(response) {
        //$('#successModal').modal('show');
        console.log("ok")
        },
        function error(e) {

            console.log(e)
        }
    );
};

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.