Facebook Android SDK 4.0+ user_location?

梦想与她 提交于 2019-12-12 03:25:27

问题


I'm trying hard to get User's country but I'm not able to get it. Not able to understand what exactly is wrong here ?

I'm referring this documentation. This is what I have tried so far -

public interface IFacebokLoginConstants extends ILoginConstants {

String KEY_FIRST_NAME = "first_name";
String KEY_LAST_NAME = "last_name";
String KEY_EMAIL = "email";
String KEY_NAME = "name";
String KEY_LOCATION = "location{location}";
String[] READ_PERMISSIONS = {"public_profile", "email", "user_friends", "user_location"};
String[] WRITE_PERMISSIONS = {"publish_actions"};
String FIELD_KEY = "fields";

String FIELD_VALUE = KEY_ID + ","
        + KEY_FIRST_NAME + ","
        + KEY_LAST_NAME + ","
        + KEY_EMAIL + ","
        + KEY_LOCATION + ","
        + KEY_NAME;
}


   //---- inside activity
    mLoginManager.logInWithPublishPermissions(this, Arrays.asList(WRITE_PERMISSIONS));

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

            Log.e(TAG, "LoginResult permissions: " + loginResult.getAccessToken().getPermissions());

            Log.e(TAG, "LoginResult Denied Permissions: " + loginResult.getAccessToken().getDeclinedPermissions());

            mUserId = loginResult.getAccessToken().getUserId();


            if (!loginResult.getAccessToken().getPermissions().contains("email")) {

                //--- current activity is  FacebookLoginActivity
            mLoginManager.logInWithReadPermissions(FacebookLoginActivity.this, Arrays.asList(READ_PERMISSIONS));
            } else {

                callUserInfoRequest(loginResult);

            }

        }

        @Override
        public void onCancel() {

            showShortToast("Login cancelled");
        }

        @Override
        public void onError(FacebookException error) {

            Log.e(TAG, "Error:  " + error.getMessage());

        }
    });

and this thing to retrieve user's info

private void callUserInfoRequest(LoginResult loginResult) {

    GraphRequest userInfoRequest = GraphRequest.newMeRequest(
            loginResult.getAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {

                    ///---- not receiving location information
                    Log.d(TAG, response.toString());


                    try {
                        mUserName = object.getString(KEY_NAME);
                        mUserFirstName = object.getString(KEY_FIRST_NAME);
                        mUserLastName = object.getString(KEY_LAST_NAME);
                        mUserEmail = object.getString(KEY_EMAIL);


                        //after userInfo is acquired get profile image..
                        callProfileImageRequest();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
    );

    Bundle parametersBundle = new Bundle();
    parametersBundle.putString(FIELD_KEY, FIELD_VALUE);
    userInfoRequest.setParameters(parametersBundle);
    userInfoRequest.executeAsync();
}

回答1:


As I didn't have access to app in Facebook console, I wasn't sure about permissions requested by app. After discussion with @MingLi in above comments, I got to know that "user_location" permission needs approval from Facebook and it was not approved, though it was requested.

Above code started working after permission was granted by Facebook.



来源:https://stackoverflow.com/questions/36306884/facebook-android-sdk-4-0-user-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!