目前我有一个代码,我的登录页面中有一个facebook登录按钮。现在,我正在尝试先检查用户是否已在点击facebook登录按钮时使用Facebook登录我的应用程序。我阅读了facebook文档 here 有关如何操作但我不知道在我的代码中应用它的位置。
loginbutton
LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions(Arrays.asList("email","public_profile"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("LoginResult : ", loginResult.getAccessToken().getToken());
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
try{
// Application code
String email = object.getString("email");
String firstName = object.getString("first_name");
String lastName = object.getString("last_name");
ArrayList<String> publicProfile = new ArrayList<String>();
publicProfile.add(email);
publicProfile.add(firstName);
publicProfile.add(lastName);
new RegisterGetList(LoginActivity.this,mProgressView,LoginActivity.this, publicProfile).execute();
}
catch (JSONException e1){
e1.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,last_name,email");
request.setParameters(parameters);
request.executeAsync();
}
答案 0 :(得分:0)
我们将获得Facebook用户帐户ID,因此从后端您必须检查userId是否已经注册。然后您就可以解决问题了。
答案 1 :(得分:0)
您可以将电子邮件存储在共享首选项中,并且可以检查共享首选项是否为空。
SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(context);
String email=sp.getString("email",null);
if(email==null)
{
//first log in
//store the email in shared prefernces
SharedPreferences.Editor editor = sharedPreferences.edit()
editor.putString("email",value);
editor.apply();
}
else
{
//already logged in
}
在用户注销时清除sp。
答案 2 :(得分:0)
在你的onCreate中检查用户是否已登录。
if (AccessToken.getCurrentAccessToken() != null) {
getUserDetails();
} else{
loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
}
并使用以下方式获取用户详细信息:
private void getUserDetails() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
Log.i(TAG, "Response >>: " + object.toString());
String userID = object.getString("id");
String strName = object.getString("name").toString();
String strgander = object.getString("gender").toString();
String strEmail = object.getString("email").toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,link,gender,age_range,cover,picture,location");
request.setParameters(parameters);
request.executeAsync();
}
在“登录”按钮的onClick中设置权限。
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
}
});
使用
将回调设置为“登录”按钮 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
最后,只需拨打&#34; getUserDetails()&#34;在onSuccess回调你的登录按钮。