Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user profile information from Facebook API Android

I'm trying to get user profile information Facebook. Begins loading, I click to confirm permission to send my profile, again, continue loading but eventually get empty fields.

What am I doing wrong? Help please.

public class SettingsApps extends Activity {
public final String API_KEY = "492429660800628";
Facebook facebook = new Facebook(API_KEY);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);

mPrefs=getPreferences(MODE_PRIVATE);
String acces_token=mPrefs.getString("acces_token",null);
long expires=mPrefs.getLong("acces_expires", 0);

if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) {
    Session.openActiveSession(this, true, new StatusCallback() { 
        public void call(Session session, SessionState state, Exception exception) {
            System.out.println("State= " + state);

            if (session.isOpened()) {
                System.out.println("Token=" + session.getAccessToken());
                Request request =  Request.newMeRequest(session, new GraphUserCallback() {       
                            public void onCompleted(GraphUser user, Response response) {                                

                                if (user != null) {
                                    TextView idText = (TextView)findViewById(R.id.id);
                                    TextView first_nameText = (TextView)findViewById(R.id.first_name);
                                    TextView last_nameText = (TextView)findViewById(R.id.last_name);
                                    idText.setText(user.getId());
                                    first_nameText.setText(user.getFirstName());
                                    last_nameText.setText(user.getLastName());
                                }
                                if (response != null) {
                                    System.out.println("Response="  + response);
                                    Toast.makeText(SettingsApps.this, response.toString(),
                                            Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                Request.executeBatchAsync(request);
            }
            if (exception != null) {
                System.out.println("Some thing bad happened!");
                exception.printStackTrace();
            }
        }
    });
}
}   
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  facebook.authorizeCallback(requestCode, resultCode, data);
  }
}

LogCat

669: INFO/ActivityManager(72): Starting: Intent { cmp=com.fitness_sport_betta/.SettingsApps } from pid 847
899: INFO/ActivityManager(72): Starting: Intent { act=SSO_WITH_FALLBACK cmp=com.fitness_sport_betta/com.facebook.LoginActivity (has extras) } from pid 847
949: INFO/System.out(847): State= OPENING
539: INFO/ActivityManager(72): Displayed com.fitness_sport_betta/com.facebook.LoginActivity: +621ms (total +851ms)
669: DEBUG/dalvikvm(389): GC_CONCURRENT freed 1685K, 41% free 6996K/11783K, external 716K/1038K, paused 16ms+11ms
420: WARN/fb4a:fb:OrcaServiceQueue(389): Exception during service
420: WARN/fb4a:fb:OrcaServiceQueue(389): com.facebook.orca.protocol.base.ApiException: remote_app_id does not match stored id 
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.protocol.base.ApiResponseChecker.b(ApiResponseChecker.java:74)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.protocol.base.ApiResponseChecker.a(ApiResponseChecker.java:103)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.protocol.base.ApiResponse.g(ApiResponse.java:184)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.katana.server.protocol.AuthorizeAppMethod.a(AuthorizeAppMethod.java:267)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.katana.server.protocol.AuthorizeAppMethod.a(AuthorizeAppMethod.java:28)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.protocol.base.SingleMethodRunner.a(SingleMethodRunner.java:125)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.katana.server.handler.PlatformOperationHandler.c(PlatformOperationHandler.java:274)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.katana.server.handler.PlatformOperationHandler.a(PlatformOperationHandler.java:175)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.server.OrcaServiceQueue.d(OrcaServiceQueue.java:214)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.server.OrcaServiceQueue.d(OrcaServiceQueue.java:37)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.server.OrcaServiceQueue$3.run(OrcaServiceQueue.java:168)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at android.os.Handler.handleCallback(Handler.java:587)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at android.os.Handler.dispatchMessage(Handler.java:92)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at android.os.Looper.loop(Looper.java:123)
420: WARN/fb4a:fb:OrcaServiceQueue(389):     at android.os.HandlerThread.run(HandlerThread.java:60)
459: INFO/ActivityManager(72): Starting: Intent { act=com.facebook.platform.PLATFORM_ACTIVITY cat=[android.intent.category.DEFAULT] cmp=com.facebook.katana/.platform.PlatformActivity (has extras) } from pid 847
639: INFO/ActivityManager(72): Starting: Intent { cmp=com.facebook.katana/.platform.PlatformWrapperActivity (has extras) } from pid 389
670: INFO/ActivityManager(72): Displayed com.facebook.katana/.platform.PlatformActivity: +1s87ms
779: INFO/ActivityManager(72): Displayed com.facebook.katana/.platform.PlatformWrapperActivity: +1s106ms
449: WARN/fb4a:fb:OrcaServiceQueue(389): Exception during service
449: WARN/fb4a:fb:OrcaServiceQueue(389): com.facebook.orca.protocol.base.ApiException: remote_app_id does not match stored id 
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.protocol.base.ApiResponseChecker.b(ApiResponseChecker.java:74)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.protocol.base.ApiResponseChecker.a(ApiResponseChecker.java:103)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.protocol.base.ApiResponse.g(ApiResponse.java:184)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.katana.server.protocol.AuthorizeAppMethod.a(AuthorizeAppMethod.java:267)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.katana.server.protocol.AuthorizeAppMethod.a(AuthorizeAppMethod.java:28)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.protocol.base.SingleMethodRunner.a(SingleMethodRunner.java:125)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.katana.server.handler.PlatformOperationHandler.c(PlatformOperationHandler.java:274)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.katana.server.handler.PlatformOperationHandler.a(PlatformOperationHandler.java:175)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.server.OrcaServiceQueue.d(OrcaServiceQueue.java:214)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.server.OrcaServiceQueue.d(OrcaServiceQueue.java:37)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at com.facebook.orca.server.OrcaServiceQueue$3.run(OrcaServiceQueue.java:168)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at android.os.Handler.handleCallback(Handler.java:587)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at android.os.Handler.dispatchMessage(Handler.java:92)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at android.os.Looper.loop(Looper.java:123)
449: WARN/fb4a:fb:OrcaServiceQueue(389):     at android.os.HandlerThread.run(HandlerThread.java:60)
469: WARN/fb4a:fb:GDPDialog(389): Failed to send
469: WARN/fb4a:fb:GDPDialog(389): com.facebook.orca.ops.ServiceException: API_ERROR: API_ERROR
469: WARN/fb4a:fb:GDPDialog(389):     at com.facebook.orca.ops.OrcaServiceOperation.c(OrcaServiceOperation.java:597)
469: WARN/fb4a:fb:GDPDialog(389):     at com.facebook.orca.ops.OrcaServiceOperation.c(OrcaServiceOperation.java:38)
469: WARN/fb4a:fb:GDPDialog(389):     at com.facebook.orca.ops.OrcaServiceOperation$2.run(OrcaServiceOperation.java:562)
469: WARN/fb4a:fb:GDPDialog(389):     at android.os.Handler.handleCallback(Handler.java:587)
469: WARN/fb4a:fb:GDPDialog(389):     at android.os.Handler.dispatchMessage(Handler.java:92)
469: WARN/fb4a:fb:GDPDialog(389):     at android.os.Looper.loop(Looper.java:123)
469: WARN/fb4a:fb:GDPDialog(389):     at android.app.ActivityThread.main(ActivityThread.java:3683)
469: WARN/fb4a:fb:GDPDialog(389):     at java.lang.reflect.Method.invokeNative(Native Method)
469: WARN/fb4a:fb:GDPDialog(389):     at java.lang.reflect.Method.invoke(Method.java:507)
469: WARN/fb4a:fb:GDPDialog(389):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
469: WARN/fb4a:fb:GDPDialog(389):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
469: WARN/fb4a:fb:GDPDialog(389):     at dalvik.system.NativeStart.main(Native Method)
009: WARN/InputManagerService(72): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40677138 (uid=10030 pid=847)
009: WARN/InputManagerService(72): Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40556e88
529: INFO/ActivityManager(72): Displayed com.fitness_sport_betta/.SettingsApps: +10s839ms

Last change:

public class SettingsApps extends Activity {

public final String API_KEY = "492429660800628";
Facebook facebook = new Facebook(API_KEY);
public final String[] permissions = {"publish_stream"};
String mUserId;
String mUserToken;
String mUserName;
String mUserEmail;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);

if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) {
    Session.openActiveSession(this, true, new StatusCallback() { 
        public void call(final Session session, SessionState state, Exception exception) {
            System.out.println("State= " + state);
            authorizeAndPostMassage(); 
            if (exception != null) {
                System.out.println("Some thing bad happened!");
                exception.printStackTrace();
            }
        }
    });
}
}

public void authorizeAndPostMassage() {

    facebook.authorize(this, permissions, new DialogListener () {
        public void onComplete(Bundle values) {
                Toast.makeText(SettingsApps.this, "Authorization successful", Toast.LENGTH_SHORT).show();
                getProfileInformation();    
        }       
        public void onFacebookError(FacebookError e) {
            Toast.makeText(SettingsApps.this, "Facebook error, try again later", Toast.LENGTH_SHORT).show();                
        }       
        public void onError(DialogError e) {
            Toast.makeText(SettingsApps.this, "Error, try again later", Toast.LENGTH_SHORT).show();             
        }       
        public void onCancel() {
            //Этот коллбэк никогда не срабатывает, вероятно, ошибка в SDK
            Toast.makeText(SettingsApps.this, "Authorization canceled", Toast.LENGTH_SHORT).show();                         
        }});        
}

public void getProfileInformation() {
    try {

        JSONObject profile = Util.parseJson(facebook.request("me"));
        Log.e("Profile", "" + profile);

        mUserId = profile.getString("id");
        mUserToken = facebook.getAccessToken();
        mUserName = profile.getString("name");
        mUserEmail = profile.getString("email");
        runOnUiThread(new Runnable() {
            public void run() {
                Log.v("FaceBook_Profile",""+mUserId+"\n"+mUserToken+"\n"+mUserName+"\n"+mUserEmail);
                Toast.makeText(getApplicationContext(),
                        "Name: " + mUserName + "\nEmail: " + mUserEmail,
                        Toast.LENGTH_LONG).show();
            }
        });

    } catch (FacebookError e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  facebook.authorizeCallback(requestCode, resultCode, data);
  }
}
like image 502
Roman Avatar asked Jan 07 '13 10:01

Roman


People also ask

How do I get my Facebook API User ID?

The simplest way to get a copy of the User Profile object is to access the /me endpoint: FB. api('/me', function(response) { }); This will this will return the users name and an ID by default.

Can I use Facebook API for free?

The Graph API is free to use, for all applicable use cases. Rate Limiting applies though, developers.facebook.com/docs/graph-api/advanced/rate-limiting There is no way to “pay” or otherwise get those limits raised for normal 3rd party apps.


1 Answers

For those who's still struggling in new Graph API 3.0 here's the solution works for Graph API 2.12 or below.

  //Request a read permission of user's info from Facebook
    //Data provided by Facebook will be used for Firebase FireStore
    LoginManager.getInstance().logInWithReadPermissions(LogIn.this, Arrays.asList("email", "public_profile"));

    LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(final LoginResult loginResult) {

            mStateOfSuccess = false;
            //Dismiss any snackbar first before showing a new one
            mSnackBar.dismiss();
            mSnackBar.show();

            Log.d(TAG, "facebook:onSuccess:" + loginResult);

            //Bundle is use for passing data as K/V pair like a Map
            Bundle bundle=new Bundle();
            //Fields is the key of bundle with values that matched the proper Permissions Reference provided by Facebook
            bundle.putString("fields","id, email, first_name, last_name, gender,age_range");

            //Graph API to access the data of user's facebook account
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("Login Success", response.toString());

                            //For safety measure enclose the request with try and catch
                            try {

                                //The get() or getString() key should be included in Bundle otherwise it won't work properly
                                //If not then error dialog will be called

                                //First re-initialize jSON object to a new Contructor with parameter that is equal to a jSON format age range
                                JSONObject ageRange  = new JSONObject(object.getString("age_range"));

                                        //Log in using Facebook with Firebase
                                        loginToFirebaseUsingFacebook(loginResult.getAccessToken()
                                                ,object.getString("first_name")
                                                ,object.getString("last_name")
                                                //Then get again get a string from object itself for the minimum age range
                                                //The idea is that we need to get minimum age only written in string format
                                                //not the whole age range data that is written in jSON format
                                                ,ageRange.getString("min")
                                                ,object.getString("gender")
                                                ,object.getString("email")
                                        );

                            }
                            //If no data has been retrieve throw some error
                            catch (JSONException e) {
                               ErrorDialog(e.getMessage(),"facebookAuth");
                            }

                        }
                    });


            //Set the bundle's data as Graph's object data
            request.setParameters(bundle);

            //Execute this Graph request asynchronously
            request.executeAsync();

        }

        @Override
        public void onCancel() {
            Log.d(TAG, "facebook:onCancel");
            ErrorDialog("Request has canceled.","facebookAuth");
        }

        @Override
        public void onError(FacebookException error) {
            Log.d(TAG, "facebook:onError", error);
            ErrorDialog(String.valueOf(error),"facebookAuth");
        }
    });


}

Note that using age range requires App Review if will use in the production, meaning you may just try to use this on your own account (admin or developers account). Visit: https://developers.facebook.com/docs/facebook-login/permissions/?locale=en_US#reference-public_profile for more details.

like image 183
Mihae Kheel Avatar answered Oct 18 '22 18:10

Mihae Kheel