我实际上正在使用firebase auth。所以我想整合facebook登录以与firebase身份验证并行工作。那么,我的问题是如果没有电子邮件它将获得电话号码,如何从当前用户获取电子邮件和电话号码。这是我的活动代码:
public class SignInActivity extends AppCompatActivity {
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private FirebaseAuth mAuth;
private FacebookCallback<LoginResult>callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
nextActivity(profile);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
mAuth = FirebaseAuth.getInstance();
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
nextActivity(newProfile);
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
nextActivity(profile);
Toast.makeText(getApplicationContext(), "Logging in ....",Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
};
loginButton.setReadPermissions("email", "public_profile","user_friends");
loginButton.registerCallback(callbackManager, callback);
}
@Override
protected void onResume() {
super.onResume();
Profile profile =Profile.getCurrentProfile();
nextActivity(profile);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
super.onActivityResult(requestCode, responseCode, intent);
callbackManager.onActivityResult(requestCode, responseCode,intent);
}
private void nextActivity(Profile profile) {
if (profile != null) {
Intent main = new Intent(SignInActivity.this, ProfileActivity.class);
main.putExtra("name", profile.getFirstName());
main.putExtra("surname", profile.getLastName());
main.putExtra("imageUrl", profile.getProfilePictureUri(200, 200).toString());
startActivity(main);
}
}}
编辑:解决方案是电话号码是Facebook api中没有电话号码。因此,为了获得电子邮件ID,我使用了这个解决方案:Facebook Android SDK 4.5.0 get email address
facebook api如何获取电子邮件和电话号码
更新:已删除user_address和user_mobile_phone权限。有关详细信息,请参阅此帖子。