我想获取用户的数据,如姓名,电子邮件和其他详细信息。我可以使用facbook API登录。但是我发现onSuccess()方法没有执行。但它显示我登录。
public class SignInUpActivity extends AppCompatActivity {
private CallbackManager callbackManager;
LoginButton fbLoginBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//intialize fb sdk
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_sign_in_up);
fbLoginBtn = (LoginButton) findViewById(R.id.ObiNoID_SignIn_SignUpActivity_btn_fb_login);
// setting permission for fb accounts
fbLoginBtn.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
callbackManager = CallbackManager.Factory.create();
fbLoginBtn.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String email = object.getString("email");
String birthday = object.getString("birthday");
Toast.makeText(SignInUpActivity.this,email+birthday,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
@Override
public void onCancel() {
Toast.makeText(SignInUpActivity.this, "cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(SignInUpActivity.this, "error", Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);// will call the call
}
}
我是初学者,请帮我解决问题。
答案 0 :(得分:0)
试试这个,
private void connectToFacebook() {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject json, GraphResponse response) {
// Application code
if (response.getError() != null) {
System.out.println("ERROR");
} else {
System.out.println("Success");
String jsonresult = String.valueOf(json);
System.out.println("JSON Result" + jsonresult);
String fbUserId = json.optString("id");
String fbUserFirstName = json.optString("name");
String fbUserEmail = json.optString("email");
String fbUserProfilePics = "http://graph.facebook.com/" + fbUserId + "/picture?type=large";
}
Log.v("LoginActivity", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
Log.v("LoginActivity", "cancel");
}
@Override
public void onError(FacebookException exception) {
// App code
// Log.v("LoginActivity", "" + exception);
Toast.makeText(LoginActivity.this, "" + exception, Toast.LENGTH_LONG).show();
}
});
}