Unable to get values ​​from Facebook GraphRequest

I use Facebook SDK 4.16.1 using Profile , I could get first name , last name , name and id my code is similar to this

 public class LoginActivity extends Activity { String id, fname, lname, email, name, gender, locale, verified; private CallbackManager callbackManager; private AccessTokenTracker accessTokenTracker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loginactivity); FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); accessTokenTracker = new AccessTokenTracker() { @Override protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) { } }; LoginButton loginButton = (LoginButton)findViewById(R.id.login_button); loginButton.setReadPermissions(Arrays.asList(Constants.FACEBOOK_PERMISSIONS)); FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { AccessToken accessToken = loginResult.getAccessToken(); Profile profile = Profile.getCurrentProfile(); FB_TOKEN=loginResult.getAccessToken().getToken(); GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response) { // Application code response.getError(); Log.e("JSON:", object.toString()); try { email = object.getString("email"); gender = object.getString("gender"); locale = object.optString("locale"); verified = object.optString("verified"); } catch (JSONException e) { e.printStackTrace(); } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,link,birthday,first_name,last_name,email,gender,verified,locale"); request.setParameters(parameters); request.executeAsync(); id = profile.getId(); fname = profile.getFirstName(); lname = profile.getLastName(); name = profile.getName(); nextActivity(profile); Toast.makeText(getApplicationContext(), "Logging in...", Toast.LENGTH_SHORT).show(); } @Override public void onCancel() { } @Override public void onError(FacebookException e) { } }; loginButton.registerCallback(callbackManager, callback); } } 

My constants look like

 public static final String[] FACEBOOK_PERMISSIONS = new String[] { "public_profile", "user_friends", "email" }; 

I am trying to get email, gender, locale and if it is already verified. How do I get other information? Mine always have zero expectation for items that receive Profile

Update

I am updating my GraphRequest as follows

 GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response) { // Application code response.getError(); Log.e("JSON-RESULT:", object.toString()); JSONObject jsonObject = null; try { jsonObject = new JSONObject(object.toString()); email = jsonObject.getString("email"); gender = jsonObject.getString("gender"); locale = jsonObject.getString("locale"); verified = jsonObject.getString("verified"); Log.e(TAG, email + " This is the email"); } catch (JSONException e) { e.printStackTrace(); } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,link,birthday,first_name,last_name,email,gender,verified,locale"); request.setParameters(parameters); request.executeAsync(); 

In my Logcat it shows that I set the email address and others correctly, but it doesn't go inside GraphRequest when you put breakpoints , and because of this, my global variable email is still null. Even if in Log.e("JSON:", object.toString()); and Log.e(TAG, email + " This is the email"); shown that I receive and set values. I am wondering how can I get the Bundle values?

+1
source share
1 answer

Try using the code below for other information .....

  private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // Log.d("Login", "onSuccess"); GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject me, GraphResponse response) { if (response.getError() != null) { // handle error } else { email = me.optString("email"); String id = me.optString("id"); String email=me.optString("email"); String name= me.optString("name"); String gender= me.optString("gender"); String verified= me.optBoolean("is_verified") + ""; } } }).executeAsync(); } @Override public void onCancel () { // Log.d("Login", "onCancel"); } @Override public void onError (FacebookException e){ // Log.d("Login", "onError " + e); } }; 
+1
source

Source: https://habr.com/ru/post/985022/


All Articles