2017-02-09 4 views
1

私は、Postmanを使用して動作することがわかっている認証エンドポイントに対して認証するためにAngularを使用しようとしています。

<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> 

getAuthorizationToken()私はHttp 400バックを取得します。 response.dataオブジェクトを調べると、エラーがerror:"unsupported_grant_type"と表示されます。 Postmanクライアントではgrant_typepasswordと指定しており、すべて正常に動作するため、これは私を混乱させます。

私は角度コードで何か間違っている必要があります。

答えて

1

最近非常に似た問題がありました。 'ヘッダ' を削除してみて、以下のように、代わりに 'dataTypeと' を挿入:

 $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' 
      } 
+0

お返事ありがとうございます。あなたが指示した変更を加えましたが、同じエラーが発生します。 – webworm

+0

代わりにヘッダーを 'Content-type'に変更できますか? 'application/x-www-form-urlencoded' –

+0

郵便配達員が使用しているものと運がないので、これも試しました。 – webworm

0

//解決=>エラー: "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) 
     } 
    ); 
}; 
関連する問題