简体   繁体   English

无法使用SDK打开Facebook会话

[英]cannot open a facebook session with the SDK

I'm trying to post a message on my Facebook wall, here is a sample of my code: 我试图在我的Facebook墙上张贴一条消息,这是我的代码示例:

 @Click (R.id.img_btn_facebook)
 @UiThread
 public void fbPostWall(){

    Session session = Session.getActiveSession();
    SessionState state = SessionState.OPENING;

    if (session == null)
      TestManager.getInstance().setActivity(getActivity()).setLoginListener(this).facebookLogin();

    Log.e("SESSION-STATUS", "" + session.getState());

    if (session != null || state.isOpened()){

        Log.e("FACEBOOK", "post link on the wall ...");
        Bundle params = new Bundle();
        params.putString("name", "This is a test");

        WebDialog feedDialog = (
                new WebDialog.FeedDialogBuilder(getActivity(),
                        session,
                        params))
                .setOnCompleteListener(new WebDialog.OnCompleteListener() {

                    @Override
                    public void onComplete(Bundle values,
                                           FacebookException error) {
                        if (error == null) {
                            // When the story is posted, echo the success
                            // and the post Id.
                            final String postId = values.getString("post_id");
                            if (postId != null) {
                                Toast.makeText(getActivity(),
                                        "Posted story, id: "+postId,
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                // User clicked the Cancel button
                                Toast.makeText(getActivity().getApplicationContext(),
                                        "Publish cancelled",
                                        Toast.LENGTH_SHORT).show();
                            }
                        } else if (error instanceof FacebookOperationCanceledException) {
                            // User clicked the "x" button
                            Toast.makeText(getActivity().getApplicationContext(),
                                    "Publish cancelled",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            // Generic, ex: network error
                            Toast.makeText(getActivity().getApplicationContext(),
                                    "Error posting story",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .build();
        feedDialog.show();
    }
}

When I click on the "img_btn_facebook" button a login session is opened if the user isn't already on Facebook. 当我单击“ img_btn_facebook”按钮时,如果用户不在Facebook上,则会打开一个登录会话。 After that, the Log.e("SESSION-STATUS", "" + session.getState()); 之后,Log.e(“ SESSION-STATUS”,“” + session.getState()); Always give SESSION-STATUS﹕ OPENING But the point is that I always get this error message from the Facebook SDK: com.facebook.FacebookException: Attempted to use a Session that was not open. 始终提供SESSION-STATUS:OPENING但要点是,我总是从Facebook SDK中收到以下错误消息:com.facebook.FacebookException:尝试使用未打开的会话。

someone can help me please to fix it? 有人可以帮我解决这个问题吗? I would like to post a message on my facebook wall thanks my Android app. 感谢我的Android应用,我想在我的Facebook墙上发布一条消息。

Did you included onActivityResult ?? 您是否包含onActivityResult?

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

and in you manifest file 并在清单文件中

<activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

and make sure that you have correct hash-key value in your facebook app 确保您在Facebook应用中具有正确的哈希键值

To generate hash-key,include following line of code in your main activity 要生成哈希键,请在您的主要活动中包含以下代码行

try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.facebook.samples.hellofacebook", 
                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));
            }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

You will get hash key in console,include that key in facebook devleopr 您将在控制台中获得哈希密钥,并将该密钥包含在facebook devleopr中

For someone having trouble with facebook sdk: 对于在Facebook SDK上遇到问题的人:

1 Download the samples , test them to see if it`s not your mobile ( change the app id on sample to match yours ); 1下载示例 ,进行测试以查看它是否不是您的手机( 更改示例上的应用程序ID以匹配您的移动设备 );

2 Dont use a activity or fragment that has other purpose for facebook login , instead create one just for that , its probably a conflict . 2。 不要使用具有其他目的的活动或片段进行Facebook登录 ,而应仅为此创建一个 活动或片段这可能是冲突的

3 Copy the facebook sample class you need (choose the activity or fragment) and call it through your code . 3 复制所需的facebook 示例类 (选择活动或片段) ,然后通过代码进行调用

4 Adapt copied class to your needs. 4使复制的课程适应您的需求。

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

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