简体   繁体   English

在Android中通过Facebook SDK实现登录的最新方法

[英]The latest method to implement login through facebook sdk in android

I am using 3.6 to implement the facebook login. 我正在使用3.6实现Facebook登录。 From the official document it is quite abstract and not easy to follow . 从官方文件来看,它是非常抽象的,不容易遵循。 So I have found some tutorial on the internet . 所以我在网上找到了一些教程。 However, when I use the code in that tutorial it said the method is deprecated. 但是,当我在该教程中使用代码时,它说该方法已被弃用。 Although it works, I afraid the offical will stop the function later. 虽然可以,但是恐怕官方会在以后停止该功能。 So, are there any way to implement the login/ post to wall / get personal info of the latest sdk ? 那么,有什么方法可以实现登录/发布到墙上/获取最新SDK的个人信息? Thanks 谢谢

The login code: 登录代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lblEmail = (TextView) findViewById(R.id.t);

    LoginButton authButton = (LoginButton) findViewById(R.id.authButton);
    authButton.setOnErrorListener(new OnErrorListener() {

        @Override
        public void onError(FacebookException error) {
            Log.i(TAG, "Error " + error.getMessage());
        }
    });
    // set permission list, Don't foeget to add email
    authButton.setReadPermissions(Arrays.asList("basic_info", "email"));
    // session state call back event
    authButton.setSessionStatusCallback(new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state,
                Exception exception) {

            if (session.isOpened()) {
                Log.i(TAG, "Access Token" + session.getAccessToken());
                Request.executeMeRequestAsync(session,
                        new Request.GraphUserCallback() {
                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {
                                if (user != null) {
                                    Log.i(TAG, "User ID " + user.getId());
                                    Log.i(TAG,
                                            "Email "
                                                    + user.asMap().get(
                                                            "email"));
                                    lblEmail.setText(user.asMap()
                                            .get("email").toString());
                                }
                            }
                        });
            }

        }
    });
}

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

In the code Request.executeMeRequestAsync is deprecate 在代码中Request.executeMeRequestAsync被弃用

Try like this FACEBOOK LOGIN NEW SDK 像这样尝试FACEBOOK登录新SDK

   Button button =(Button) findViewById(R.id.button_share);
 button.setOnClickListener(new OnClickListener() {   
 @Override
 public void onClick(View v) {    
if (fb_session != null && fb_session.isOpened()) {
  makeMeRequest(fb_session);     
  Log.i("Facebook Login State == >", "Facebook Login State");     
 } else {
  if (fb_session == null) {
   fb_session = new Session(mCurrContext);            
       }
  Session.setActiveSession(fb_session);
  ConnectToFacebook();
   Log.i("Facebook not Login State == >", "Facebook Not login State");
 }         
 }
});  
 }

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

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