无法登录FB Android SDK

时间:2014-08-03 14:54:47

标签: android facebook-login facebook-sdk-3.14.x

哇,FB SDK su ***很大的时间。 我花了太多时间试图弄清楚为什么简单的登录不起作用。 " HelloFacebookSample"工作正常并使用我的帐户登录到FB但是当我尝试在我的应用程序中实现它时,我什么都没得到!

我实际上复制粘贴了他们写的整个活动,只是为了看看它是否会起作用,他们为什么这么麻烦呢?

当我打印出"会话"这就是我得到的:

08-03 17:49:02.074: D/SESSION(30213): {Session state:OPENING, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}, appId:

基本上,我有"登录Facebook按钮"设置,当我点击它时,它尝试连接但没有成功。

以下是活动:

public class FacebookLogin extends Activity {

    private Button postStatusUpdateButton;
    private Button postPhotoButton;
    private Button pickFriendsButton;
    private Button pickPlaceButton;
    private LoginButton loginButton;
    private ProfilePictureView profilePictureView;
    private TextView greeting;
    private ViewGroup controlsContainer;
    private GraphUser user;
    private GraphPlace place;
    private List<GraphUser> tags;
    private boolean canPresentShareDialog;
    private boolean canPresentShareDialogWithPhotos;
    private PendingAction pendingAction = PendingAction.NONE;

    private enum PendingAction {
        NONE,
        POST_PHOTO,
        POST_STATUS_UPDATE
    }

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    private FacebookDialog.Callback dialogCallback = new FacebookDialog.Callback() {
        @Override
        public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
            Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
        }

        @Override
        public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
            Log.d("HelloFacebook", "Success!");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.fb_login);

    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);



    loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
        @Override
        public void onUserInfoFetched(GraphUser user) {
            FacebookLogin.this.user = user;
            updateUI();
            // It's possible that we were waiting for this.user to be populated in order to post a
            // status update.
           // handlePendingAction();
        }
    });

    profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);
    greeting = (TextView) findViewById(R.id.greeting);



    }

    private void updateUI() {
        Session session = Session.getActiveSession();

        if(session==null){
            session = new Session.Builder(getApplicationContext()).setApplicationId(getString(R.string.app_id)).build();
        }
        session = Session.getActiveSession();
        Log.d("SESSION", session+"");

       // Log.d("SESSION", session.toString()+"");
        boolean enableButtons = (session != null && session.isOpened());
        Log.d("SESSION_BOOL", enableButtons+"");


        if (enableButtons && user != null) {
            profilePictureView.setProfileId(user.getId());
            greeting.setText(getString(R.string.hello_user, user.getFirstName()));
        } else {
            profilePictureView.setProfileId(null);
            greeting.setText(null);
        }
    }

    private interface GraphObjectWithId extends GraphObject {
        String getId();
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (pendingAction != PendingAction.NONE &&
                (exception instanceof FacebookOperationCanceledException ||
                exception instanceof FacebookAuthorizationException)) {
                new AlertDialog.Builder(FacebookLogin.this)
                    .setTitle("Cancel")
                    .setMessage("Permissions not granted")
                    .setPositiveButton(R.string.ok, null)
                    .show();
            pendingAction = PendingAction.NONE;
        } else if (state == SessionState.OPENED_TOKEN_UPDATED) {
            handlePendingAction();
        }
        updateUI();
    }

    @SuppressWarnings("incomplete-switch")
    private void handlePendingAction() {
        PendingAction previouslyPendingAction = pendingAction;
        // These actions may re-set pendingAction if they are still pending, but we assume they
        // will succeed.
        pendingAction = PendingAction.NONE;

    }


}

添加到Manifest:

  <meta-data
       android:name="com.facebook.sdk.ApplicationId"
       android:value="@string/app_id" />

请帮助我理解我错过了什么?

1 个答案:

答案 0 :(得分:1)

您需要添加清单

<activity
            android:name="com.facebook.LoginActivity"
            android:label="@string/app_name" />

您还需要在按下登录按钮时添加权限列表

public static final List<String> PERMISSION = Arrays.asList("email",
            "public_profile");

loginButton.setReadPermissions(PERMISSION);