如果超级安卓应用程序在Facebook登录时获取我的手机号码
我在我的Android应用程序中使用Facebook登录,我想从Facebook获取用户手机号码,但我不知道以下是我的Android代码
private void FbInitialise() {
loginButton = (LoginButton) findViewById(R.id.fb_login);
callbackManager = CallbackManager.Factory.create();
loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
graphRequest(loginResult.getAccessToken());
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
graphRequest(loginResult.getAccessToken());
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
}
public void graphRequest(AccessToken token) {
GraphRequest request = GraphRequest.newMeRequest(token, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
LoginType="FB";
String firstName = "";
String lastName = "";
String stremail = "";
String strmobile = "";
if (object.has("first_name")) {
firstName = object.getString("first_name");
}
if (object.has("last_name")) {
lastName = object.getString("last_name");
}if (object.has("email")) {
stremail = object.getString("email");
}
Intent intent=new Intent(SelectSocilaLoginActivity.this,ConfirmInfoActivity.class);
intent.putExtra("firstName",firstName);
intent.putExtra("lastName",lastName);
intent.putExtra("email",stremail);
intent.putExtra("parent","SocialLogin");
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle b = new Bundle();
b.putString("fields", "id, first_name, last_name, email,gender, birthday, location");
request.setParameters(b);
request.executeAsync();
}
请给我适当的解决方案
答案 0 :(得分:-1)
以下是我设法从登录用户的个人资料中提取手机号码的方法:
public class UserAuth extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
private Context context;
private Button btnViewProfile;
//For User data
private String userId, userFirstName, userMiddleName, userLastName, userProfileImage, userEmail, userGender, userMobile, userBirthdate;
//For Facebook
private CallbackManager callbackManager;
LoginButton loginButton = null;
private ImageButton btnFacebook;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Enabling strict mode */
setStrictThreadMode();
//initializing facebook sdk
FacebookSdk.sdkInitialize(getApplicationContext());
//Initialize your view components available in the layout
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken,
AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile,
Profile newProfile) {
if (newProfile != null) {
userId = newProfile.getId();
userFirstName = newProfile.getFirstName();
userMiddleName = newProfile.getMiddleName();
userLastName = newProfile.getLastName();
userProfileImage = newProfile.getProfilePictureUri(400, 400).toString();
}
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
}
//To enable single thread at a time mode
private void setStrictThreadMode() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
protected void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
@Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
}
//This method initializes the facebook login button, sets the required permissions and changes its default background to our drawable
//It also registers the callback for the Login button
private void registerLoginButton() {
List<String> mPermissions = new ArrayList<>();
mPermissions.add("public_profile");
mPermissions.add("email");
mPermissions.add("user_birthday");
mPermissions.add("user_friends");
mPermissions.add("user_mobile_phone");
loginButton.setReadPermissions(mPermissions);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult result) {
GraphRequest request = GraphRequest.newMeRequest(result.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
userEmail = object.optString("email");
userGender = object.optString("gender");
userMobile = object.optString("mobile_phone");
String userBirthdate = object.optString("birthday");
logoutCurrentUser();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
}
//This method is to end the current users session
private void logoutCurrentUser() {
LoginManager.getInstance().logOut();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
观察我们已添加了获取手机号码的权限:mPermissions.add(“user_mobile_phone”);
此外,请记住,如果用户将其公开,您将只能获得手机号码。
希望这适合你。