krisrak / jquery-cordova-oauth2

jQuery plugin for doing Oauth2 login in a Cordova App
MIT License
66 stars 34 forks source link

Problem to return token #25

Closed FedeM closed 7 years ago

FedeM commented 7 years ago

Recently I've been trying to make a system login with Facebook to use it in Indel XDK and I am unable to get the token once you've logged.

I'm using the sample code and adapted:

function oauth2_login_facebook () {
            $.oauth2({
                auth_url: 'https://www.facebook.com/dialog/oauth',
                response_type: 'token',
                client_id: 'XXXXXXXXXXXXXXXXXX',
                redirect_uri: 'http://localhost/cliente/www/app.html',
                other_params: {scope: 'basic_info', display: 'popup'}
                }, function(token, response){
                    $("#logs").append("<p class='success'><b>access_token: </b>"+token+"</p>");
                    $("#logs").append("<p class='success'><b>response: </b>"+JSON.stringify(response)+"</p>");
                }, function(error, response){
                    $("#logs").append("<p class='error'><b>error: </b>"+JSON.stringify(error)+"</p>");
                    $("#logs").append("<p class='error'><b>response: </b>"+JSON.stringify(response)+"</p>");
            }); 
}

The problem is that I console.log in function (token, response) to see the token obtained but only reedirecciona me redirect_uri window.

I'm new to the subject and would not know Oauth well as theme redirect_uri, if would have to have a specific code or the address where you would only have to go once authentication has been made.

krisrak commented 7 years ago

Have you added the InAppBrowser plugin? If you are testing in XDK emulator, instead of localhost use some domain, like www.example.com, it can be anything.

FedeM commented 7 years ago

I have already included the InAppBrowser plugin but not know how to access the data.

Apparently the system is running since the first time I tried it I opened the window logon in the same app and now only redirect me to start redirect_uri address.

I did a test to put any other direction and appears this:

"The redirect_uri URL must be absolute".

I was also watching guide Using Intel "Oauth 2.0 login for HTML5 apps" and in "Get the access_token from the redirected URL hash fragment" contains the following code:

$(loginWindow).on('loadstart', function(e) {
    var url = e.originalEvent.url;
    var access_token = url.split("access_token=")[1];
    ...
}

That code would not know how to use it since I hit in the event function (token, response) but never executed.

krisrak commented 7 years ago

the login window should not open in same app, it should popup a modal view in the app, there should be a back button when login screen is loaded, if not your InAppBrowser is not setup correctly

FedeM commented 7 years ago

The login window opens in a popup. My problem is not how to get user data in the case of using Facebook to get "basic_info" and in the case of Google "profile".

I apologize for being so upset, I have never used these functions.

FedeM commented 7 years ago

I managed to send the access token to the server, now the problem is that it is not sending the state and will not let me access to the data, the error:

"Facebook SDK returned an error: Cross-site request forgery validation failed. Required param "state" missing."

How I can do to send the state?

krisrak commented 7 years ago

specify it in other_params: {scope: 'basic_info', display: 'popup', state: '123'}

FedeM commented 7 years ago

Thank you very much for the prompt response, I can give as solve my problem.