简体   繁体   English

我的角度应用程序中的错误请求代码400

[英]BAD REQUEST code 400 in my angular app

I am getting a bad request from the api call i'm making and i have no idea why, been searching around for a long time now. 我正在从api调用中收到一个错误的请求,我不知道为什么,已经搜索了很长时间。 the response i get from the call is : 我从电话中得到的回复是:

{"error":"invalid_request","error_description":"Missing input parameters"}

So i'll list my code below, first my html form: 因此,我将在下面列出我的代码,首先是我的html表单:

    <ion-view view-title="Login" id="login-page">
  <ion-content id="login-page">
    <h1 id="title">Welkom</h1>
    <img src="../img/logo.png" alt="logo" id="logo">
    <form>
      <div class="list list-inset">
          <label class="item item-input">
            <input type="email" ng-model="email" name="email" placeholder="email">
          </label>
          <label class="item item-input">
            <input type="password" ng-model="password" name="password" placeholder="wachtwoord">
          </label>
      </div>

            <button class="button button-block button-balanced" type="submit" id="form-btn" ng-click="login()">Log in</button>
          <button class="button button-block button-stable" type="submit" id="form-btn">Registreer</button>
          <button class="button button-block button-dark" type="submit" id="scan-btn">Scan</button>
          <a href="templates/browse.html">test</a>
    </form>
  </ion-content>
</ion-view>

then my service: 然后我的服务:

    angular.module('starter.services', [])

.factory('loginFactory',  function($http, $q){        
    return {
        login: function(email, password) {
            var deferred = $q.defer();
            var data = {
            grant_type: 'password',
            username: email,
            password: password,
            client_id: 'GingerwaldUserApp15',
            client_secret: 'mySecretKey'
    };
            var config = {
                headers: {
                    'Content-Type': 'application/json'
            }};
            var url = "https://www.gingerwald.com/community/v2.1/authorization/oauth/token.php";

            $http.post(url, data, config)
                .success(function(respons){
                    deferred.resolve(respons);
                })
                .error(function(respons,status) {
                    deferred.reject(respons);
                })
                return deferred.promise;
        }
    }
})

and last my controller: 最后是我的控制器:

angular.module('starter.controllers', [])

.controller('LoginCtrl', function($scope, $location, $localStorage, loginFactory) {
    $scope.login = function() {
      var email = $scope.email;
      var password = $scope.password;
     //test BAD REQUEST 400 console.log(email, password)
      loginFactory.login(email, password)
      .then(function(response){
        console.log(response);
        $localStorage.token = response.access_token;
        $stage.go("app.browse")
      }, function(error) {
          $scope.email = '';
          $scope.password = '';
      } )
    }
});

my app.js is just the routing. 我的app.js只是路由。 so i'm trying to get a token, because i have to use this token in order to execute other api calls. 所以我想获取令牌,因为我必须使用此令牌才能执行其他api调用。

this is the api info: 这是api信息:

 <table> <tr> <th>description</th> <th>parameters</th> <th>response</th> </tr> <tr><td>To request an access token, based on the user's credentials (email and password). The returned access token has to be used in all other API calls in order to get authorization. The webservice needs to called with method "POST"</td> <td>'grant_type="password"</br> username=the user's email address</br> password=the user's password</br> client_id=the app's id</br> client_secret=the app's secret key</td> <td>The access token is a string of 64 characters. The access token also has an expiration time. The token can only be used until the expiration time has passed.</td></tr> </table> 

thank you 谢谢

For any experiening this problem in the future: after plenty of hours of debugging i found the solution: This was literally the only solution that worked, i tried many many syntaxes of $http. 对于将来遇到的任何问题:经过大量的调试,我找到了解决方案:这实际上是唯一可行的解​​决方案,我尝试了许多$ http语法。 but only this specific one worked 但是只有这个特定的一个有效

.factory('loginFactory',  function($http, $q, $localStorage){        
    return {
        login: function(email, password) {
            var deferred = $q.defer();
            var config = {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
            }};
            var url = "https://www.gingerwald.com/community/v2.1/authorization/oauth/token.php";
            $http.post(url, "username=" + encodeURIComponent(email) + 
                            "&password=" + encodeURIComponent(password) + 
                            "&grant_type=password" +
                            "&client_id=GingerwaldUserApp15" +
                            "&client_secret='secretKey'", config)
                .success(function(respons){
                    deferred.resolve(respons);
                })
                .error(function(respons,status) {
                    deferred.reject(respons);
                    console.log()
                })
                return deferred.promise;
        }
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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