简体   繁体   English

如何在登录Loggin Facebook(android)时仅设置电子邮件权限

[英]How to set only email permissions when Loggin Facebook (android)

I'm working on an App that I want to use Facebook to login. 我正在开发一个要使用Facebook登录的应用程序。 But I only want to ask for "email" permission at the beginning, and then "publish_actions" when user wants to publish. 但是我只想在开始时请求“电子邮件”许可,然后在用户要发布时请求“ publish_actions”。 I followed the tutorial for developpers in facebook site, but it doesn't work as I expected. 我在Facebook网站上遵循了针对开发人员的教程,但并未按预期工作。 My problem is with the first login. 我的问题是第一次登录。 This is the code of my LoggingFragment: 这是我的LoggingFragment的代码:

import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.facebook.Session;
import com.facebook.Session.OpenRequest;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LoginButton;


public class LoginFragment2 extends Fragment {

private static final List<String> public_permissions = Arrays.asList("publish_actions");
private static final List<String> basic_permissions = Arrays.asList( "email");
private boolean pendingPublishReauthorization = false;
private static final String TAG = "LoginFragment2";
private UiLifecycleHelper uiHelper;
App app;
Boolean mLogged = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("LOGIN", "LoginFragment onCreate ");
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
    app = ((App) getActivity().getApplication());
    mLogged = app.isLoggedIn();
    app.addHandler(mHandler);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_login_activity2, container, false);

    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
    authButton.setFragment(this);
    authButton.setReadPermissions(basic_permissions);

    LinearLayout login_screen = (LinearLayout) view.findViewById(R.id.login_screen);
    RelativeLayout loading_screen = (RelativeLayout) view.findViewById(R.id.loading_screen);

    if (app.isLoggedIn()) {
        login_screen.setVisibility(View.GONE);
        loading_screen.setVisibility(View.VISIBLE);
    } else {
        login_screen.setVisibility(View.VISIBLE);
        loading_screen.setVisibility(View.GONE);
    }

    return view;
}

@Override
public void onResume() {
    super.onResume();
    Log.d("LOGIN", "LoginFragment onResume ");

    if (mLogged) {
        OpenRequest openrequest = new OpenRequest(this).setPermissions(basic_permissions);
        // For scenarios where the main activity is launched and user
        // session is not null, the session state change notification
        // may not be triggered. Trigger it if it's open/closed.
        // Session session = Session.getActiveSession();
        Session session = new Session(getActivity());
        session.openForRead(openrequest);
        if (session != null && (session.isOpened() || session.isClosed())) {
            onSessionStateChange(session, session.getState(), null);
        }

        if (session != null) {
            // OpenRequest openrequest = new
            // OpenRequest(this).setPermissions(basic_permissions);
            // session.openForRead(openrequest);
            Log.d("LOGIN", "Session!=null ");
            Log.d("LOGIN", "Session state: " + session.getState());
            Log.d("LOGIN", "Session spermissions: " + session.getPermissions());
        }
        // } else{
    }
    Session session = Session.getActiveSession();
    if (session != null &&
           (session.isOpened() || session.isClosed()) ) {
        onSessionStateChange(session, session.getState(), null);
    }

    uiHelper.onResume();
    // }

}

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

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {

        app.LoginApp("facebook:" + session.getAccessToken());
        app.setSessionFacebook(session);
    } else if (state.isClosed()) {
        Log.d("LOGIN", "Logged out...");
    }
}

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

private MsgHandler mHandler = new MsgHandler(this);

private MsgHandler msgHandler = new MsgHandler(this);

private static class MsgHandler extends WeakReferenceHandler<LoginFragment2> {
    public MsgHandler(LoginFragment2 fragment) {
        super(fragment);
    }

    @Override
    public void handleMessage(LoginFragment2 mfragment, Message msg) {

        switch (msg.what) {
        /**************/

        }
    }
}

The problem is that, as you will se, I try to set permissions to "email" in the onCreateView method, setting it to the authButton. 问题是,正如您将要看到的,我尝试在onCreateView方法中将权限设置为“电子邮件”,并将其设置为authButton。 However, when I sign in, session return has much more permissions that this, including "publish" permissions. 但是,当我登录时,会话返回具有的权限更多,包括“发布”权限。 I don't know what I'm doing wrong since I just follow Facebook instructions, and make call to its SDK just as they say. 我不知道我在做什么错,因为我只是按照Facebook的说明进行操作,并按照他们的说法调用其SDK。

Does anyone has had this problem ? 有人遇到过这个问题吗? Any ideas of what's going on? 有什么想法吗? Thanks in advance 提前致谢

Maybe I should also say that the Dialog that ask user to confirm permissions shows the correct ones, so "public info, friend list and email" , that is basic info and email, the ones that I wanted. 也许我也应该说,要求用户确认权限的对话框显示了正确的对话框,因此“公共信息,朋友列表和电子邮件”即我想要的基本信息和电子邮件。 Then, tha callback tat call onSessionChange returns a session with many more permissions 然后,回调tat调用onSessionChange返回具有更多权限的会话

If you've already granted the app publish permissions in the past, then those permissions carry over to the next session (no matter which device or website you're on), regardless of what initial permissions the app is currently asking for. 如果您过去曾经授予过该应用程序发布权限,则无论该应用程序当前要求的是什么初始权限,这些权限都将延续到下一个会话(无论您在哪个设备或网站上)。

Basically, you're getting a superset of the permissions you're asking for because you've already given the app MORE permissions in the past. 基本上,您将获得所要求的权限的超集,因为您过去已经为应用程序提供了更多权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM