我刚开始使用facebook api所以如果我的问题很容易解决,请不要讨厌我。 我正在开发一个登录FB的Android应用程序。我有关于devFB的教程中的FB api设置。 一切顺利,按下登录按钮,FB登录界面显示为弹出窗口,您可以登录等。 但是当在设备上有一个FBapp然后我的代码重定向到这个应用程序进行授权,然后挂起什么都不做,零,null。
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(final Session session, SessionState state, Exception exception) {
// make request to the /me API
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(R.id.instructionsOrLink);
welcome.setText("Hello "+ user.getName() + "!");
它是教程代码的副本。
有任何建议如何防止这种情况?
答案 0 :(得分:1)
我正在使用此代码,也许您应该尝试一下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(final Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
emailAddress = user.getProperty("email").toString();
name = user.getName();
fbId = user.getId();
Communication.loginWithFb(FacebookLoginActivity.this, user.getId());
}
}
});
} else if (session.getState().equals(SessionState.CLOSED_LOGIN_FAILED)) {
Toast.makeText(FacebookLoginActivity.this, getResources().getString(R.string.login_with_facebook_error), Toast.LENGTH_LONG).show();
finish();
}
}
}, null);
}
private Session openActiveSession(Activity activity, boolean allowLoginUI, StatusCallback callback,
List<String> permissions) {
OpenRequest openRequest = new OpenRequest(activity).setPermissions(permissions).setCallback(callback);
Session session = new Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
public void onDataDownloaded(CommunicationOutput<UserData> output) {
...
}