我基本上在Android应用中实现了使用Facebook登录。我完成了developers.facebook.com上给出的步骤,并完成了那里给出的所有内容。但是在我的代码中没有调用onCompleted()方法,所以如何在成功登录后检索用户信息。任何帮助将不胜感激!
以下是我的java代码
package com.example.rohitsuthar.facebook_signin;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import org.json.JSONObject;
import java.security.MessageDigest;
public class MainActivity extends AppCompatActivity {
CallbackManager callbackManager;
AccessToken mAccessTokem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.d("ROHIT","inside on success");
mAccessTokem=loginResult.getAccessToken();
Log.d("ROHIT","token retrived");
GraphRequest graphRequest=GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
//hi
Log.d("ROHIT","oncompleted is called");
}
});
}
@Override
public void onCancel() {
// App code
Log.e("ROHIT","inside onCancel");
}
@Override
public void onError(FacebookException exception) {
// App code
Log.e("ROHIT",exception.toString());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}
这是我的日志
06-13 15:00:41.238 32721-32721/com.example.rohitsuthar.facebook_signin D/ROHIT: inside on success
token retrived
06-13 15:00:41.239 32721-32721/com.example.rohitsuthar.facebook_signin D/com.facebook.FacebookSdk: getGraphApiVersion: v3.0
06-13 15:00:41.244 32721-32721/com.example.rohitsuthar.facebook_signin D/AccessTokenTracker: AccessTokenChanged
在检索到令牌后,没有任何事情发生,请告诉我发生了什么以及如何解决这个问题!
答案 0 :(得分:2)
在定义graphRequest.executeAsync();
对象后,您必须在onSuccess()
方法内调用GraphRequest
,您的代码应如下所示:
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.d("ROHIT","inside on success");
mAccessTokem=loginResult.getAccessToken();
Log.d("ROHIT","token retrived");
GraphRequest graphRequest=GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
//hi
Log.d("ROHIT","oncompleted is called");
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email"); // Here you add the parameters you need from user facebook account
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
@Override
public void onCancel() {
// App code
Log.e("ROHIT","inside onCancel");
}
@Override
public void onError(FacebookException exception) {
// App code
Log.e("ROHIT",exception.toString());
}
});
答案 1 :(得分:1)
在onSuccess()
中调用此方法,它将适合您
@Override
public void onSuccess(LoginResult loginResult) {
getUserDetails(loginResult)
}
方法是
private void getUserDetails(LoginResult loginResult) {
GraphRequest data_request = GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject json_object,
GraphResponse response) {
//here is your login is success now your requirement is start From here
//do your work
Log.e("String Response from Facebook:>",json_object.toString())
}
});
Bundle permission_param = new Bundle();
//width and height is optional parameters
permission_param.putString("fields", "id,name,email,picture.width(120).height(120)");
data_request.setParameters(permission_param);
data_request.executeAsync();
}
让我知道它是否适用于您或此处的任何其他问题。