【问题标题】:angular post to obtain access token failed获取访问令牌的角度帖子失败
【发布时间】:2023-03-07 04:47:01
【问题描述】:

你们能帮我创建一个 angularjs POST 请求版本

https://drive.google.com/file/d/0B6weCtBHDZU1eUFWcHpNNE5WTVk/view?usp=sharing.

这个想法是获取访问令牌,以便我可以安全地使用一些休息 api。 我通过angular的帖子尝试了很多次,我分析了每个请求,但我失败了,我总是发现请求正文是空的。

这是我尝试过的一些角码的示例。

    var app = angular.module("app", []);
    app
        .controller(
                "MyController",
                function($scope, $http) {
                    $scope.obtain_token = function() {
                        var payload = "grant_type=password" + "&username=roy" + "&password=spring" +
                          "&client_id=clientapp"+
                          "&client_secret=123456";
                        var r = $http.post('http://localhost:8080/oauth/token',payload);
                        r.success(function(response){
                            console.log(response.access_token);
                        });
                    };
                });

【问题讨论】:

    标签: java angularjs rest http oauth


    【解决方案1】:
        function Hello($scope, $http) {
    
        var config = {
                headers : {
                    'Accept': 'application/json',
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Authorization': 'Basic ' +btoa('clientapp:123456654321')
                }
            }
        var payload ="password=spring&username=roy&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp";
    
        $http.post('http://localhost:8080/oauth/token',payload, config).
        success(function(data, status, headers, config) {
            $scope.data = data;
        }).
        error(function(data, status, headers, config) {
            $scope.data = data; 
        }); 
    }
    

    【讨论】:

    • 您还应该解释什么是错误的以及您所做的更改。不仅发布功能。 (并更好地格式化)
    • 他忘记了标题,你可以从他附加的谷歌驱动器中看到图片
    • adsl Le 建议的代码在同一个应用程序中运行良好。问题是我尝试使用的资源托管在 localhost:8080 上,而我试图从托管在 localhost:8090 的另一个应用程序请求它。前台可以吗?
    【解决方案2】:
    sample angular-1 code..
    ----------------------------------
    var payload = {
    "grant_type":"password",
    "username"="roy",
    "password"="spring",
    "client_id"="clientapp"
    "client_secret"="123456"
    }
    
    $http.post("http://localhost:8080/oauth/token", payload).success(function (result) {
                    console.log(result);
                }).error(function(error) {
                    console.log("error", error);
                });
    
    and sample api c#
    ---------------------
    [HttpPost]
            [Route("")]
            public HttpResponseMessage SaveArchive([FromBody] JObject saveRequest)
            {
                var log = LogManager.GetCurrentClassLogger();
                try``
                {
                    return "your success message";
                }
                catch (Exception ex)
                {
    
                    return ex;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2014-02-03
      • 1970-01-01
      • 2016-05-20
      • 2022-09-26
      • 2011-07-28
      • 1970-01-01
      • 2016-07-27
      • 2019-04-06
      • 2017-10-27
      相关资源
      最近更新 更多