我正在嘗試使用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_type
爲password
,並且所有工作都按預期工作。
我必須在Angular代碼中做錯事。
感謝您的回覆。我做了你指出的改變,但我得到了同樣的錯誤。 – webworm
你可以嘗試,而不是改變頭'內容類型':'應用程序/ x-www-form-urlencoded' –
我試過這也是郵差使用,沒有運氣。 – webworm