2

我尝试在我的应用程序中使用 facebook 身份验证,但我无法登录,我总是被重定向到询问我是否允许应用程序访问我的信息的表单(即我的代码中的“dologin”方法..这是我的代码:

package com.fbconnect;

import android.app.Activity;
import android.os.Bundle;
import com.facebook.*;
import com.facebook.model.*;
import com.facebook.widget.*;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;

public class MainActivity extends Activity {

    private TextView textInstructionsOrLink;
    private Button buttonLoginLogout;

    private Session.StatusCallback statusCallback = new SessionStatusCallback();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        buttonLoginLogout = (Button)findViewById(R.id.login_button);
        setContentView(R.layout.main);
        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
            }
        }

        updateView();

    }

    @Override
    public void onStart() {
        super.onStart();
        Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    public void onStop() {
        super.onStop();
        Session.getActiveSession().removeCallback(statusCallback);
    }

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

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }

    private void updateView() {
        Session session = Session.getActiveSession();
        Log.i("session log", session.toString());
        if (session.isOpened()) {
            TextView welcome = (TextView) findViewById(R.id.welcome);
            welcome.setText("Hello !");
        } 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);
        }
    }

    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            updateView();
        }
    }
}
4

1 回答 1

0

问题实际上来自密钥哈希我在 Windows 上遇到了几个愚蠢的问题,这里有一些建议:

  • 不要使用windows cmd,通过cygwin来做
  • 不要使用 %HOMEPATH% (即使它在 facebook 上显示...),更喜欢长距离:“C:\Users\...\.android\debug.keystore”
  • 用引号括住您的密钥库路径(即使其中没​​有空格)
  • 如果生成器没有询问您密码,它将无法工作,您在命令中做错了
于 2013-01-17T23:48:05.513 回答