1

我在我的应用程序中使用 facebook sdk 3。我想获取一些要在我的应用程序中使用的用户信息。如果我在我的设备中安装了 facebook 应用程序,它工作得很好。它要求登录,如果它没有登录并且可以工作。但即使设备上没有安装 facebook 应用程序,我也希望我的应用程序能够正常工作。我该怎么做。我的代码如下。请帮忙

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();
            }
        }
    });
4

2 回答 2

1
// 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) {

}

在 onCreate 上添加此代码并从 facebook 获取哈希键并在 facebook 上的应用程序设置中替换此键

于 2013-03-05T04:45:15.810 回答
0

在 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>

现在,每当您单击 facebook 登录按钮时,它会打开 facebook 应用程序(如果已安装),否则会在 webview 中打开 facebook 登录页面

创建

@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

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());
             }                  
    });
 }
于 2013-03-04T12:59:27.340 回答