ccoenraets / OpenFB

Micro-library that lets you integrate browser and Cordova apps with Facebook with no plugin or SDK dependency.
MIT License
505 stars 231 forks source link

User Email-id not fetching. #86

Open sivaonline opened 8 years ago

sivaonline commented 8 years ago

After login though openFB, i didnt get the Emailid ,gender and first_name.

    openFB.api({
    path: '/me',
    success: function(data) {
              console.log(JSON.stringify(data));
              var name = data.name;
              var fbid = data.id;
             var username = data.first_name;
              var emailid = data.email;
              var gender = data.gender;
              alert('success');                            
              },
              error: errorHandler
        });
    function errorHandler(error) {
        alert(error.message);
    }

Please, Any suggestion guys

guarani commented 8 years ago

It only works when I test using the App ID (465374093524857) in the example: http://coenraets.org/apps/openfb/index.html So it seems that it's a problem with the way we configure the Facebook app.

richardshergold commented 8 years ago

I'm getting nothing when trying to use the openFB.api call either. Did you solve this? What do I need to do to configure my Facebook app to allow this?

MacGWik commented 8 years ago

me too. when i use my app id, openFB only fetch the user_id and the name...

and now, i need to fetch email and birthday

MacGWik commented 8 years ago

Pls help us the developer of openFB... :(

MacGWik commented 8 years ago

Before you read this i assume that your app facebook can get the "Name" and "ID" property

Hello all, i have tried every way and, i get the solution...

the solution is make the link graph api hardcoded (I'm not make a variable) to get the value that you want..

look at the picture ! No 1. change the "v2.5" as th graph api version that you use no 2. change the fields value ("id,name,email,birthday") to fields that you need to get from the user

that's all untitled

abiodunadigun commented 8 years ago

it is very simple. you don't need to change anything in the openfb.js file. what you need to do is to change the call method in the index.html file

<script>
openFB.init({ appId: 'your-app-id' });
function login() {
    openFB.login(function (response) {
        if (response.status === 'connected') {
            if (response.authResponse) {
                var accessToken = response.authResponse.accessToken;
                openFB.api({
                    path: "/{your app version in facebook developer envi.}/me?",
                    params: { "access_token": accessToken, "?fields":"name,email,gender,user_birthday,locale,bio&access_token='"+accessToken+"'" },
                    success: function (response) {
                        var data = JSON.stringify(response);
                       // do whatever you want with the data for example
                       $(".data_div").html(data); 
                       $(".data_email").html(data.email);

                    },
                    error: function(err){
                    alert(err);
                   }
                });
            }
            else { 
                   alert(“empty response returned”);

            }
        }
        else { 
                alert(“not connected”);
        }
    },
    {
        scope: "public_profile,email,user_birthday"
    });
}
</script>

very important! Please replace {your app version in facebook developer envi.} with your app version like v2.0 or v2.5

also don't forget to change 'your app id' to you facebook app id example 432046826485152

avegpatekar commented 8 years ago

I tried above 2 solutions but getting error while retriving the email id i.e "an active access token must be used to query information about the current user javascript". Can anyone suggest a solution?

abiodunadigun commented 8 years ago

could you please write out a code sample on how you tried it? maybe we could help figuring out whats wrong with your code

avegpatekar commented 8 years ago

Here is the code, `openFB.login(function (response) { if (response.status === 'connected') { if (response.authResponse) { var accessToken = response.authResponse.accessToken; openFB.api({ path: "/v2.5/me?", params: { "access_token": accessToken, "?fields":"first_name,last_name,email,&access_token='"+accessToken+"'" }, success: function (response) { var data = JSON.stringify(response);

            },
            error: function(err){
            alert(JSON.stringify(err));
           }
        });
    }
    else { 
           alert('empty response returned');

    }
}
else { 
        alert('not connected');
}

}, { scope: "public_profile,email" });`

I am able to get the first_name and last_name but not able to get email id.

abiodunadigun commented 8 years ago

try it by changing this params: { "access_token": accessToken, "?fields": "first_name,last_name,email,&access_token='" + accessToken + "'" },

to this

params: { "access_token": accessToken, "?fields": "first_name,last_name,email&access_token='" + accessToken + "'" },

that is removing the coma after the fields parameter email.

avegpatekar commented 8 years ago

Removed coma. Still facing same issue.

abiodunadigun commented 8 years ago

in your openfb.js does the function api(obj){...} looks like the below code ?

` function api(obj) {

    var method = obj.method || 'GET',
        params = obj.params || {},
        xhr = new XMLHttpRequest(),
        url;

    params['access_token'] = tokenStore.fbAccessToken;

    url = 'https://graph.facebook.com' + obj.path + '' + toQueryString(params);

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                if (obj.success) obj.success(JSON.parse(xhr.responseText));
            } else {
                var error = xhr.responseText ? JSON.parse(xhr.responseText).error : {message: 'An error has occurred'};
                if (obj.error) obj.error(error);
            }
        }
    };

    xhr.open(method, url, true);
    xhr.send();
}`
avegpatekar commented 8 years ago

Yes it is exactly same.

abiodunadigun commented 8 years ago

on my end the code is working perfectly .

abiodunadigun commented 8 years ago

this is what i used:

a b

avegpatekar commented 8 years ago

You are calling with v2.0. Where as my app version is 2.5. Let me try with 2.0.

abiodunadigun commented 8 years ago

okay. maybe that will help

avegpatekar commented 8 years ago

Still not able to get the email id field even after changing the version. I am trying with this app id '940903142645622'. Is there any configuration need to make through app settings. In settings it shows persmission granted for email, public_profile and user_friends.

abiodunadigun commented 8 years ago

try to see if accessKey is treu by testing from you code this line:

success: function (response) { alert(accessToken); }

if you can see the alerted accessToken then the code should be okay. maybe the user email address is not being verified by facebook. I assumed so because you said you were able to get the firstname and lastname

Mushaqdeen commented 8 years ago

This works for me.... In openfb.js

function api(obj) {

    var method = obj.method || 'GET',
        params = obj.params || {},
        xhr = new XMLHttpRequest(),
        url;

    params['access_token'] = tokenStore['fbtoken'];
    params['fields'] = 'id,name,email';

    url = 'https://graph.facebook.com' + obj.path + '?' + toQueryString(params);
abiodunadigun commented 8 years ago

Nice . I am happy for you:)

chalingui commented 8 years ago

MacGWik solution works for me. Thanks!

francolamber commented 8 years ago

the same of chalingui, MacGWik solution works for me, and really thank you very much for the help , I was very useful

psatriaw commented 8 years ago

wew.. this just about the trust for the library, we just need to make sure if the library works properly, because in these case, i need to add some code and it bypassing the code inside the library. but it works!!! thanks!

ibadrehman90 commented 7 years ago

How can I get the email address in an alert or in a textfield while using MacGWik methods? Please help.

MacGWik commented 7 years ago

just read my comment at the top @ibadrehman90

I've tried it and it works

*I make it hardcoded because I don't undestand how to make a variable for it, so If, your API version is different with the picture, please change it to yours.

In my case, I use API version 2.5 and I give permission on the facebook app to get the user email*

rcd229 commented 7 years ago

I found a sort of hybrid approach that worked for me:

1) hard-code version into openfb.js like in MacGWik's screenshot but DO NOT hardcode fields url = 'https://graph.facebook.com/**v2.6**' + obj.path + '?' + toQueryString(params);

2) use the "params" field in openFB.api as in abiodunadigun's code, but remove the &access_token='"+accessToken+"'"} part at the end and the ? before fields so that it just looks like:

params: { "access_token": accessToken, "fields":"name,email,gender,user_birthday,locale,bio" }

guilhermemauro commented 7 years ago

The @MacGWik's solution works for me.

martinskou commented 7 years ago

This was the solution for me:

        openFB.api({
            path: '/v2.8/me',
            params: { "access_token": accesstoken, "fields":"id,name,email" },
            success: function(data) {
mehulnahar commented 6 years ago

Mushaqdeen solution Works to me Thanks a lot :) :) :)

In openfb.js

function api(obj) {

var method = obj.method || 'GET',
    params = obj.params || {},
    xhr = new XMLHttpRequest(),
    url;

params['access_token'] = tokenStore['fbtoken'];
params['fields'] = 'id,name,email';

url = 'https://graph.facebook.com' + obj.path + '?' + toQueryString(params);
daksamedia commented 4 years ago

@martinskou thank you! your solution bring my smile back!