我已将我的应用与facebook登录集成。这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Session.openActiveSession(this, true, new Session.StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView t = (TextView) findViewById(R.id.text);
t.setText("Hello " + user.getName() + "!");
}
}
});
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}`
在设备模拟器中此代码有效,但是当我在我的设备中尝试时,该应用程序会向我显示以下消息: 所以,当我点击确定时,textView没有设置用户名。为什么呢?
答案 0 :(得分:0)
我需要额外的阅读权限,因此我的代码如下所示:
OpenRequest req = new OpenRequest(getActivity()).setCallback(new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
//do login stuff
} else {
//show error message
}
}
});
} else {
if (exception != null) {
//show error
}
}
}
});
req.setPermissions(Arrays.asList("email", "user_location"));
Session session = new Builder(getActivity()).build();
Session.setActiveSession(session);
session.openForRead(req);
这是在Fragment中,因此您可以将getActivity()
替换为this
。
使用正确的onActivityResult()方法,我对FB没有任何问题。