user4325333
user4325333

Reputation:

Getting "Invalid format" error during server side auth in Oauth.io

I have a javascript client and a laravel backend and I am using Oauth.IO for social authentication. I followed the steps specified here: http://docs.oauth.io/#authorizing-the-user-with-both-front-end-and-back-end-sdks

I successfully got state token by calling generateStateToken() method, but when I send that to $this->oauth->auth() method I get Invalid Format error. Can you please tell me what this error means and what I am doing wrong.

Client Side

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

Server Side

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

-- snip --

// 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();

I have verified that $code does have the exact state token that I have received on the 1st step. The value of $credentials is as follows:

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

Please help me out here. This error occurs for both for facebook and twitter as well. Let me know if you need more details.

Upvotes: 2

Views: 519

Answers (1)

user4325333
user4325333

Reputation:

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

Upvotes: 1

Related Questions