我的应用程序有问题,我正在尝试实现facebook登录,我在developer.facebook.com上阅读了指南,我写了这段代码
public class LoginActivity extends Activity {
private CallbackManager callbackManager;
private Boolean facebookLogin = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_login);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
String userID = loginResult.getAccessToken().getUserId();
String facebookToken = loginResult.getAccessToken().getToken();
facebookLogin = true;
sendLoginData(userID, facebookToken);
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "onCancel", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException e) {
e.printStackTrace();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplicationContext(), "onActivityResult", Toast.LENGTH_SHORT).show();
callbackManager.onActivityResult(requestCode, resultCode, data);
}
我也像导游说的那样修改清单,但是,当我运行应用程序时,如果我点击Facebook登录按钮,我的应用程序将关闭,没有任何明显的错误。
我也尝试执行调试,我在facebook回调内部和onActivityResult()中放置了一些断点但是还没有到达。
如果我在loginButton.setReadPermissions()中添加一些权限,应用程序会打开facebook活动,但是当我点击确定时,会再次关闭。
任何人都可以帮助我?
答案 0 :(得分:0)
尝试使用此代码。
loginButton = (LoginButton) findViewById(R.id.login_button);
List < String > permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {@Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity",
response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
name = object.getString("name");
email = object.getString("email");
gender = object.getString("gender");
birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}