0
我正在努力獲取用戶的國家,但我無法得到它。無法理解這裏究竟有什麼問題?Facebook Android SDK 4.0+ user_location?
我指的是this文檔。這是我到目前爲止已經試過 -
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());
}
});
這個東西來獲取用戶的信息
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();
}
檢查此http://stackoverflow.com/a/32073757/3535399 –
@vivektakrani已經嘗試過此解決方案 - 位置{位置}在上面的代碼中提到的註釋中提到 –
首先嚐試您的請求在Graph API Explorer中,並看看什麼正好你回來了,以什麼結構。 https://developers.facebook.com/tools/explorer – CBroe