简体   繁体   English

Facebook登录问题我的应用程序是Android

[英]Facebook login troubleI app Android

I have the following class that I instantiate from my fragment: 我有以下从片段中实例化的类:

public class FacebookLogin {
    private CallbackManager mCallbackManager;
    private LoginButton lButton;
    private AccessToken accessToken;
    private Profile profile;
    private static final String TAG = "FacbookLogin";

   public FacebookLogin(Context c) {
        FacebookSdk.sdkInitialize(c);
        mCallbackManager = CallbackManager.Factory.create();
        Log.i(TAG, " Constructor called");
    }

    private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {

            Log.i(TAG, " logged in...");

            AccessToken accessToken = loginResult.getAccessToken();
            profile = Profile.getCurrentProfile(); //Access the profile who Is the person login i
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {
            Log.i(TAG, " Error");
            Log.e(TAG, e.getMessage());
        }

    };

   public void setCallback(LoginButton lButton) {
       lButton = lButton;
       lButton.registerCallback(mCallbackManager, mCallback);
       Log.i(TAG, " setCallback called");
   }

    public CallbackManager getCallbackManager(){

        return mCallbackManager;
    }

    public Profile getProfile() {
        return profile;
    }
}

And here Is my fragment-class: 这是我的片段类:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fLogin = new FacebookLogin(getActivity().getApplicationContext());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v =  inflater.inflate(R.layout.login, container, false);
    return v;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
    super.onViewCreated(view, savedInstanceState);

    final LoginButton loginButton = (LoginButton) getView().findViewById(R.id.login_button);
    final TextView infoText = (TextView) getView().findViewById(R.id.text_details);

    loginButton.setFragment(this);


    loginButton.setReadPermissions("user_friends");

    loginButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i(TAG, " Button clickde");
            fLogin.setCallback(loginButton); //Let's register a callback
            profile = fLogin.getProfile();
            if (profile != null) {
                Log.i(TAG, profile.getName());
            } else {
                Log.i(TAG, "NULL....... inside onresume");
            }
        }
    });

}

As you can see, Im trying to get the users profile when you click on the button. 如您所见,当您单击按钮时,我试图获取用户个人资料。 The login works, but the problem I have Is that when I click Login, else-statement Is executed and prints out NULL...... However, when I click Logout, I get the correct information, and the profile-object Is no longer NULL. 登录有效,但是我遇到的问题是,当我单击“登录”时,执行else语句并打印出NULL……。但是,当我单击“注销”时,我得到了正确的信息,并且配置文件对象是不再为NULL。

Why Is It null the first time I click on the button (login), and then becomes true when I click on It again (logout)? 为什么第一次单击按钮(登录)时它为null,然后再次单击(注销)时它为true?

When I place the profile-code inside onResume, It works. 当我将配置文件代码放在onResume中时,它可以工作。 Why? 为什么?

   @Override
    public void onResume()
    {
        super.onResume();
        profile = fLogin.getProfile();
        if (profile != null) {
            Log.i(TAG, profile.getName()); //Prints out directly when I hit the button
        } else {
            Log.i(TAG, "NULL....... inside onresume");
        }
    }

You need to use Facebook Graph Api for getting user details from facebook. 您需要使用Facebook Graph Api从Facebook获取用户详细信息。 Here is the login code I used for my project. 这是我用于项目的登录代码。

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            /**
             * Current SDK uses GraphAPI to retrieve data from facebook
             */
            final String token = loginResult.getAccessToken().getToken();
            GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                    String name1 = jsonObject.optString("name");
                    String email1 = jsonObject.optString("email");
                    JSONObject cover = jsonObject.optJSONObject("cover");
                    String coverPic = cover.optString("source");
                    String id = jsonObject.optString("id");
                    String userdp = "https://graph.facebook.com/" + id + "/picture?type=large";
                    loginSocial(Constants.POST_METHOD_FACEBOOK, token);
                }
            });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,cover");
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getApplicationContext(), "Login Cancelled... Please try again later", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(FacebookException exception) {
            Toast.makeText(getApplicationContext(), "Login Failed... Please try again later", Toast.LENGTH_LONG).show();
        }
    });

Use an interface for solving this. 使用界面来解决这个问题。

In the FacebookLogin class 在FacebookLogin类中

OnFacebookListener facebookListener;
public interface OnFacebookListener{
    void onFacebookLoggedIn(Profile profile);
}
public void setFacebookListener(OnFacebookListener onfacebooklistener){
    this.facebookListener=onfacebooklistener;
}

and then 接着

    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        profile = Profile.getCurrentProfile();
        facebookListener.onFacebookLeggedIn(profile);
    }

then in your fragment 然后在你的片段

fLogin.setFacebookListener(new OnFacebookListener(){
    @Override
    public void onFacebookLoggedIn(Profile profile){
      //Do whatever you want to do with profile
    }
});

Hope this works for you :D 希望这对您有用:D

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

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