WordPress OAuth获取错误400

时间:2016-02-02 07:47:08

标签: javascript json ajax wordpress oauth

我正在使用WordPress Rest API,我正在下载这个名为 WP OAuth服务器的插件(由Justin Greer提供),我已经构建了自己的OAuth连接。

我遇到一个问题:我收到错误400,并说:The grant type was not specified in the request

到目前为止,这是我的代码:

HTML:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="assets/css/app.css" type="text/css">
</head>
<body>
<div id="result"></div>
<!--<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=places"></script>-->
<script type="text/javascript" src="assets/js/app.js"></script>
</body>
</html>

JavaScript的:

/**
 * OAUTH 2 LOGIN
 */

+function () {
    var $CLIENT_CODE = 'fc8uhbwlo4niqhngrjsdl3tbp3cndpidfs61w77g';
    var $CLIENT_ID = 'llsfdwZzO7qVHBzM4nhfcq1jFW2L8O';
    var $CLIENT_SECRET = 'auaCKn8JWXQmSyYrl3PDi23klIhotp';

    var $AUTHORIZATION_ENDPOINT = 'http://domain.dev/oauth/authorize';
    var $TOKEN_ENDPOINT = 'http://domain.dev/oauth/token';


    $.ajax({
        url: $AUTHORIZATION_ENDPOINT + '?client_id=' + $CLIENT_ID + '&client_secret=' + $CLIENT_SECRET + '&response_type=code',
    }).done(function (url) {
        $('#result').html(url);
        fetchSomething();
    }).fail(function (errorThrown) {
        console.log("Error" > errorThrown.responseText);
    })


    function fetchSomething() {
        $.ajax({
            type: 'POST',
            url: $TOKEN_ENDPOINT + '?grant_type=authorization_code&code=' + $CLIENT_CODE,
        }).done(function (success) {
            console.log(success);
        }).fail(function (error) {
            console.log(error);
        });
    }
}();

CodePen

1 个答案:

答案 0 :(得分:0)

这是我的解决方案

$.ajax({
    url: 'http://' + domain + '?oauth=token',
    type: 'POST',
    data: {
        grant_type: grantType,
        code: code,
        client_id: clientID,
        client_secret: clientSecret,
        redirect_uri: redirect
    }
}).done(function (token) {
    console.log(token);
}).fail(function (fail) {
    console.log(fail.responseJSON.error);
});