Facebook android SDK重新打开会话

时间:2013-01-17 09:33:35

标签: android facebook

我正在开发一个facebook集成的应用程序,我遇到了一个非常奇怪的问题。

我正常登录,但是当我即将退出时,SDK会再次强制登录...

package com.machinarius.facebook;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.Request;
import com.facebook.Request.GraphUserCallback;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Session.NewPermissionsRequest;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

public class MainActivity extends Activity implements StatusCallback, GraphUserCallback {
    private TextView statusText;
    private Button loginButton;
    private Boolean loggedIn;
    private Session actualSession;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loggedIn = false;
        statusText = (TextView)findViewById(R.id.statusText);
        loginButton = (Button)findViewById(R.id.loginButton);
    }

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

    public void toggleFacebookLogin(View source) {
        if(!loggedIn) {
            actualSession = Session.openActiveSession(this, true, this);
        }
        else 
            if(actualSession == null)
                return;
            else {
                actualSession.close();
                statusText.setText("Please login");
                loggedIn = false;
            }
    }

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if(state.equals(SessionState.OPENED)) {
            loggedIn = true;
            List<String> permissions = getNewPermissions(actualSession.getPermissions(), 
                    new LinkedList<String>(Arrays.asList("user_about_me", "user_birthday", "user_hometown", "user_relationships", "user_location", "user_relationships", "email")));
            Session.NewPermissionsRequest permissionsRequest = new NewPermissionsRequest(this, permissions);
            actualSession.requestNewReadPermissions(permissionsRequest);
            populateInterface();
        } else {
            loggedIn = false;
        }
    }

    private void populateInterface() {
        Request.executeMeRequestAsync(Session.getActiveSession(), this);
    }

    @Override
    public void onCompleted(GraphUser user, Response response) {
        if(response.getError() != null) {
            statusText.setText("There was an error fetching your info");
            Log.e(getClass().getName(), response.getError().toString());
            return;
        }
        if(user != null) {
            statusText.setText("Hello " + user.getName() + "\n"
                    + "Your user ID is " + user.getId() + "\n"
                    + "Your birthday is " + user.getBirthday());
            loginButton.setText("Log out");
        } else {
            statusText.setText("There was an error fetching your info");
        }
    }

    private List<String> getNewPermissions(List<String> actualPermissions, List<String> desiredPermissions) {
        ArrayList<String> permissionsGranted = new ArrayList<String>();
        for(String nPermission : desiredPermissions)
            for(String aPermission : actualPermissions)
                if(nPermission.equals(aPermission))
                    permissionsGranted.add(nPermission);
        for(String gPermission : permissionsGranted)
            desiredPermissions.remove(gPermission);
        return desiredPermissions;
    }
}

toggleFacebookLogin由loginButton上的XML onClick处理程序调用。

另一个问题是,如何将权限请求集成到session.open调用中,这样我就不必让用户两次看到Facebook对话框了?

1 个答案:

答案 0 :(得分:4)

这里的问题是你只是在检查

state.equals(SessionState.OPENED)

在您的call方法中而不是

state.isOpened()

因此,当您请求其他权限时,状态实际上会转换为OPENED_TOKEN_UPDATED,现在您的loggedIn状态为false(即使它应该为true)。因此,当您单击该按钮时,您再次调用openActiveSession,因为您的loggedIn状态不正确。如果您切换到检查state.isOpened(),它应该可以解决您的问题。

要回答您的第二个问题,因为您只是请求读取权限,您可以在打开会话时立即请求所有权限(而如果您还想要请求发布权限 - 例如publish_stream,那么您需要分开请求)。要一次完成所有操作,您需要将Session.openActiveSession(this, true, this)替换为:

Session actualSession = new Session(this);
Session.setActiveSession(actualSession);
actualSession.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("some", "read", "permissions")).setCallback(this));

注意:我在上面写了上面的内容,因此可能存在语法错误。