Capture user data from Facebook

5

I'm trying to collect with JAVASCRIPT / JQUERY some specific user data that is not part of my friends list. I would like to capture data as: likes , friends , posts and groups .

I did some testing and saw no difference in whether or not I logged into Facebook. The result is almost always given to JSON empty:

{
    data: [ ]
}

In some cases it returns the correct data, but most apply in case of emptiness.

A specific case happens with friends :

{
    error: {
        message: "Unsupported operation",
        type: "FacebookApiException",
        code: 100
    }
}

The tests are done via url:

https://graph.facebook.com/{user-id}/{edges}?access_token={access_token}

Where:

{user-id} = userid (My is 100002309392122);

{edges} = data that I want to collect said at the beginning of the question and that are listed in facebook documentation .

{access_token} = is the access key that the API needs to get the data.

Access token was generated by me via an application that I just created to generate the same. Concatenating the application ID with the pipe character and then using the App Secret generated the token .

I wanted to change this scenario to " JSON empty". Am I doing something wrong? Can not get this data? #Comofaz?

    
asked by anonymous 02.05.2014 / 19:45

1 answer

6

You're making a little mess with the access token.

This access_token is generated when the user explicitly authorizes their application to access the requested data.

To get the user's data, it is necessary that he access the page in question and authorize the app. I do this:

/*Esta função verifica se o usuário está logado no facebook. 
Se não estiver ele abre a janelinha de login*/
    function initFacebook() {
      FB.getLoginStatus(function(response) {
        if (response.status !== fbConnected) {
          loginFacebook(); 
        }
      });  
    }

/*Esta função vai recuperar tudo que você solicitou do usuário.*/
function carregarInformacoes() {
  FB.getLoginStatus(function(response) {
    if (response.status === fbConnected) {
      FB.api('/me', {}, function(response) {
        /*Response tem tudo que você solicitou, inclusive o access_token.*/
      });
    } else if (response.status === fbNotAuthorized) {
      loginFacebook(); 
    } else {
      loginFacebook();          
    }
  });  
}

/*Esta função pede permissão de acesso aos dados. Ela que no fim das contas vai gerar o access_token*/
function loginFacebook() {
  FB.login(function(response) {
    if (response.authResponse) {
      initFacebook();
    }       
  }, {scope: 'email, user_photos, friends_about_me, read_friendlists, user_education_history, user_groups, user_interests, user_likes, friends_likes, user_work_history, user_online_presence' });
}

Note that in loadingInformations I use path / me. This is done in this way to get current user data. Once authorized you can save the access_token to a database of life.

After that you can use the approach you are trying.

    
02.05.2014 / 22:51