gpt4 book ai didi

android - Facebook SDK 3.0 Android - 缺少 token

转载 作者:行者123 更新时间:2023-11-29 01:55:09 26 4
gpt4 key购买 nike

我正在尝试在我的应用程序中使用 facebook 身份验证,但我无法登录,因为缺少 token 。在 LogCat 中我有一个循环:“[Tag]System.out [Text]doLogin:”。这发生在 Facebook 对话框之前,当它出现时我单击“确定”但没有任何反应。

这是我的代码:

private SharedPreferences.Editor loginPrefsEditor;
private Session.StatusCallback statusCallback = new SessionStatusCallback();


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fb_auth);
choosePseudoButton = (Button) findViewById(R.id.buttonChoosePseudoFb);
progressBarFb = (ProgressBar) findViewById(R.id.progressBarFbLoad);
textViewFbInfo = (TextView) findViewById(R.id.currentInfoFb);
editTextFbPseudo = (EditText) findViewById(R.id.editTextPseudoFb);
/*
* Get existing access_token if any
*/
mPrefs = getPreferences(MODE_PRIVATE);

// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
public void call(final 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) {
System.out.println("onCreate");
fbToken = session.getAccessToken();
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", fbToken);
JSONRequest jr = (JSONRequest) new JSONRequest().execute();

}
}
});
}
else {
doLogin();
}
}
});
}

private void doLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
}
fbToken = session.getAccessToken();
System.out.println("doLogin: "+fbToken);
JSONRequest jr = (JSONRequest) new JSONRequest().execute();
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

private void updateView() {
Session session = Session.getActiveSession();
if (session.isOpened()) {
fbToken = session.getAccessToken();
System.out.println("updateView: "+fbToken);
JSONRequest jr = (JSONRequest) new JSONRequest().execute();
}
else {
doLogin();
}
}

private class SessionStatusCallback implements Session.StatusCallback {
public void call(Session session, SessionState state, Exception exception) {
// Respond to session state changes, ex: updating the view
if( session.isOpened() ){
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

public void onCompleted(GraphUser user, Response response) {
}
});
}
updateView();
}
}

最佳答案

注意几点:

session 打开是异步发生的,这意味着当您调用 Session.openActiveSession 时,它不会(总是)立即打开 session (它必须调用另一个 Activity ,等待用户输入等)。所以你的线

fbToken = session.getAccessToken();

几乎总是返回 null。

其次, session 在进入打开状态之前可以处于几种状态,因此在您的 SessionStatusCallback 中,您应该将 updateView() 调用移动到 if(session.isOpened()) 检查中.

最后,您将在 onCreate 方法中调用 Session.openActiveSession,并且在该回调中,如果 session 未打开,您将始终调用 doLogin()。但正如我之前所说, session 在打开之前可以转换为多个状态,因此您实际上是在多次调用 doLogin。删除 doLogin() 调用。

关于android - Facebook SDK 3.0 Android - 缺少 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15615362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com