简体   繁体   中英

failed to obtain access token from angular script

i'm trying to obtain some access tokens to consume some rest apis hosted in distant application, i successfully obtained the tokens when i sent the request from the curl agent and from some plugins like Open HttpRequester, and i successfully consumed the rest api.

But when i tried to request the token from an angular script function i get the 401 unauthorized message.

i would like an equivalent of this curl request in an angular js post method.

curl -X POST -vu clientapp:123456 http://localhost:8080/oauth/token -H  "Accept: application/json" -d "password=spring&username=roy&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp".

I don't know how include the clientapp:123456 pair in the post angular method.

This is the script

    var app = angular.module("app", []);
    app.controller("MyController", function($scope, $http) {
        $scope.obtain_token = function() {
            $http({
                method : 'POST',
                url : 'http://clientapp:123456@localhost:8080/oauth/token',
                data: {
                        username:'roy',
                        password: 'spring',
                        client_id: 'clientapp',
                        client_secret: '123456',
                        scope: 'write'
                      },
                transformRequest: function(obj) {
                    var str = [];
                    for (var p in obj)
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        return str.join("&");
                }}).success(function (data, status, headers, config){
                    alert(data.access_token);
                }).error(function (data, status, headers, config){
                    //alert("Errooooooooor");
                });
        };
    });

I tried your version but it's not working.

You can post to url:

http://clientapp:123456@localhost:8080/oauth/token

if use HTTP Authentication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM