简体   繁体   English

android:使用facebook sdk 3登录facebook,未安装facebook应用

[英]android : facebook login using facebook sdk 3 with no facebook app installed

I am using facebook sdk 3 in my app. 我在我的应用程序中使用了Facebook SDK 3。 I want to fetch some user info to be used in my app. 我想获取一些要在我的应用中使用的用户信息。 It is working good if i have the facebook app installed in my device. 如果我在设备中安装了Facebook应用程序,则效果很好。 It ask for login, if it's not logged in and works. 它要求登录(如果未登录且可以正常运行)。 But i want my app to work even if no facebook app is installed on the device . 但是,即使设备上未安装任何Facebook应用程序,我也希望我的应用程序能够正常工作。 How can i do it. 我该怎么做。 My code is below. 我的代码如下。 Please help 请帮忙

Session.openActiveSession(this, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (session.isOpened()) {
                // make request to the /me API
                Request.executeMeRequestAsync(session,
                        new Request.GraphUserCallback() {

                            // callback after Graph API response with user
                            // object
                            @Override
                            public void onCompleted(GraphUser graphuser,
                                    Response response) {
                                L
                                if (graphuser != null) {
                                    GraphObject graphObject = response
                                            .getGraphObject();
                                    Log.i(" facebookLogin Email is ",
                                            "value"
                                                    + graphObject
                                                            .getProperty("email"));
                                    User user = getUserDetails(graphObject,
                                            graphuser.getId());
                                    sendFBCredentialsToServer(user);
                                    updateSharedPrefsWithFacebookCredentials(user);

                                }
                            }

                        });
                Toast.makeText(getApplicationContext(),
                        "Pls wait , Fetching data", Toast.LENGTH_LONG)
                        .show();
            }
        }
    });
// Add code to print out the key hash


try {
PackageInfo info = getPackageManager().getPackageInfo(
        "com.your.packagename", 
        PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(signature.toByteArray());
    Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));// g
    }
} catch (NameNotFoundException e) {

} catch (NoSuchAlgorithmException e) {

}

add this code on onCreate and get the Hash key from facebook and replace this key on app settings on facebook 在onCreate上添加此代码,并从Facebook获取哈希键,并在Facebook上的应用设置上替换此键

Add Facebook LoginButton in your main.xml 在main.xml中添加Facebook LoginButton

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.facebook.widget.LoginButton
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login With Facebook" />

</LinearLayout>

Now, whenever you click on facebook loginbutton it open facebook app if installed otherwise open facebook login page in webview 现在,每当您单击facebook loginbutton时,它会打开facebook应用程序(如果已安装),否则在webview中打开facebook登录页面

onCreate 的onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

   LoginButton authButton = (LoginButton) view.findViewById(R.id.loginButton);
   authButton.setReadPermissions(Arrays.asList("email","user_about_me"));

}

onSessionStateChange onSessionStateChange

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
             if( session.isOpen())
                  getUserData();

}
public void getUserData()
{
Request.executeMeRequestAsync(Session.getActiveSession(), 
                              new GraphUserCallback() {

        @Override
        public void onCompleted(GraphUser user, Response response) {

            Log.d("FNAME", user.getFirstName());
            Log.d("LNAME", user.getLastName());
            Log.d("GENDER",user.getProperty("gender").toString());
            Log.d("EMAIL_ID",user.getProperty("email").toString());
            Log.d("FACEBOOK_ID",user.getId());
             }                  
    });
 }

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

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