2017-02-09 102 views
1

我正在嘗試使用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"。這讓我感到困惑,因爲在郵差客戶端中,我指定grant_typepassword,並且所有工作都按預期工作。

我必須在Angular代碼中做錯事。

回答

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

你可以嘗試,而不是改變頭'內容類型':'應用程序/ 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) 
     } 
    ); 
}; 
相關問題