在我的角应用

时间:2017-01-27 00:42:25

标签: angularjs web-services rest http http-headers

我收到来自api电话的错误请求,我不知道为什么,现在已经搜索了很长时间。我从电话中得到的回应是:

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

所以我将在下面列出我的代码,首先是我的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>

然后我的服务:

    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;
        }
    }
})

并持续我的控制器:

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 = '';
      } )
    }
});

我的app.js只是路由。所以我试图获得一个令牌,因为我必须使用这个令牌才能执行其他api调用。

这是api信息:

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

谢谢

1 个答案:

答案 0 :(得分:0)

对于将来遇到此问题的任何经验:经过大量的调试后我找到了解决方案: 这实际上是唯一有效的解决方案,我尝试了许多$ http的语法。但只有这个特定的工作

.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;
        }
    }
})