2

我有一个 javascript 客户端和一个 laravel 后端,我正在使用 Oauth.IO 进行社交身份验证。我按照此处指定的步骤操作:http: //docs.oauth.io/#authorizing-the-user-with-both-front-end-and-back-end-sdks

我通过调用generateStateToken()方法成功获得了状态令牌,但是当我将它发送到$this->oauth->auth()方法时,我得到了Invalid Format错误。你能告诉我这个错误是什么意思,我做错了什么。

客户端

var selectedAuth = 'facebook';
$.post('http://localhost/auth/v1/social', {provider: selectedAuth, get_state_token: 1}, function(data){
    OAuth.popup(selectedAuth)
    .done(function(result) {
        console.log(result);
        $.post('http://localhost/auth/v1/social', {provider: selectedAuth, code: data.token, access_token: result.access_token}, function(data){
            console.log(data);
        }, 'json');
    })
    .fail(function (err) {
        //handle error with err
    });
}, 'json');

服务器端

// code to get the state token in a different method
$token = $this->oauth->generateStateToken();
return response()->json(['status' => 'success', 'token' => $token]);

-- 剪辑 --

// code to get access token from state token in another method
$provider = 'facebook';
$request_object = $this->oauth->auth($provider, array(
    'code' => $code
));
$credentials = $request_object->getCredentials();

我已经验证$code确实有我在第一步收到的确切状态令牌。的值$credentials如下:

{"status":"error","data":{"code":"Invalid format"},"refreshed":false}

请帮帮我。facebook 和 twitter 也会出现此错误。如果您需要更多详细信息,请告诉我。

4

1 回答 1

1

I am really sorry for posting this question. I had missed a step in the documentation. I am leaving this question here as a reference for others in case anyone else faces the same issue.

The error was in my javascript code. While calling Oauth.popup(), I needed to pass the state token and get a code which was then supposed to be passed to the server. I was sending the state token to the server directly instead of the code.

The correct code will be this :

var selectedAuth = 'facebook';
$.post('http://localhost/auth/v1/social', {provider: selectedAuth, get_state_token: 1}, function(data){
    OAuth.popup(selectedAuth, {
        state: data.token
    })
    .done(function(result) {
        console.log(result);
        $.post('http://localhost/auth/v1/social', {provider: selectedAuth, code: result.code, access_token: result.access_token}, function(data){
             console.log(data);
        }, 'json');
    })
    .fail(function (err) {
        //handle error with err
    });
 }, 'json');
于 2015-07-25T16:46:56.287 回答