Facebook登录和Post To Wall仅限第一次使用

时间:2014-08-18 05:44:25

标签: android facebook facebook-android-sdk

我有一个奇怪的情况。我正在尝试将Facebook与我的应用程序集成。但是登录和发布到墙上只是第一次工作。我很震惊了过去2天,但找不到解决方案。

以下是代码段:

//.............Facebook..........................

private static final String APP_ID = "My app id";
// Instance of Facebook Class
    private Facebook facebook = new Facebook(APP_ID);
    private AsyncFacebookRunner mAsyncRunner;

    private SharedPreferences mPrefs;
    String access_token;
    long expires;

//............Facebook..............................//

                else if(actionId == ID_FACEBOOK)
                {
                    mAsyncRunner = new AsyncFacebookRunner(facebook);
                    filename=myList.get(posglobal).get("foldername");

                    mPrefs = getPreferences(MODE_PRIVATE);
                     access_token = mPrefs.getString("access_token", null);
                    expires = mPrefs.getLong("access_expires", 0);

                    if (access_token != null) {
                        facebook.setAccessToken(access_token);

                        Log.d("FB Sessions", "" + facebook.isSessionValid());
                    }

                    if (expires != 0) {
                        facebook.setAccessExpires(expires);
                    }

                    if (!facebook.isSessionValid()) {
                        facebook.authorize(BoonDriveActivity.this,
                                new String[] { "email", "publish_stream" },
                                new DialogListener() {

                                    @Override
                                    public void onCancel() {
                                        // Function to handle cancel event
                                    }

                                    @Override
                                    public void onComplete(Bundle values) {
                                        // Function to handle complete event
                                        // Edit Preferences and update facebook acess_token
                                        final SharedPreferences.Editor editor = mPrefs.edit();
                                        editor.putString("access_token",
                                                facebook.getAccessToken());
                                        editor.putLong("access_expires",
                                                facebook.getAccessExpires());
                                        editor.commit();
                                        LayoutInflater inflater=BoonDriveActivity.this.getLayoutInflater();
                                        View layout=inflater.inflate(R.layout.createsharedialoglinkedin,null);
                                        final AlertDialog d1 = new AlertDialog.Builder(BoonDriveActivity.this)
                                        // Your other options here ...
                                        .setView(layout)
                                        .create();
                                        d1.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                                        d1.show();
                                        lntxtfilename=(TextView)layout.findViewById(R.id.txtfilename);
                                        lnetmessage=(EditText)layout.findViewById(R.id.et_message);
                                        ln_btn_share=(Button)layout.findViewById(R.id.btn_share);
                                        ln_btn_showlink=(Button)layout.findViewById(R.id.btn_showlink);
                                        lnshowlink=(EditText)layout.findViewById(R.id.et_showlink);

                                        lntxtfilename.setText("Share"+" "+filename+" "+"with:");

                                        ln_btn_share.setOnClickListener(new View.OnClickListener() {

                                            @Override
                                            public void onClick(View v) {
                                                // TODO Auto-generated method stub
                                                postToWall();

                                            }
                                        });



                                    }

                                    @Override
                                    public void onError(DialogError error) {
                                        // Function to handle error

                                    }

                                    @Override
                                    public void onFacebookError(FacebookError fberror) {
                                        // Function to handle Facebook errors

                                    }

                                });
                    }



                }

我正在对自定义视图进行充气以便在墙上发布。第一次 access_token的值为空到期时为零。但是当我尝试分享时第二次没有任何反应。我调试了我的应用程序并发现现在access_token和expires都有一些值,条件 if(!facebook.isSessionValid())没有触发。任何帮助??我知道我正在使用一些弃用的方法,但我找不到更新的教程或文档。

以下是发布到墙上的代码:

   /**
     * Function to post to facebook wall
     * */
    @SuppressWarnings("deprecation")
    public void postToWall() {
     // post on user's wall.
     facebook.dialog(this, "feed", new DialogListener() {

      @Override
      public void onFacebookError(FacebookError e) {
      }

      @Override
      public void onError(DialogError e) {
      }

      @Override
      public void onComplete(Bundle values) {
      }

      @Override
      public void onCancel() {
      }
     });

    }

更新:

我正在使用此代码生成哈希:

 // Add code to print out the key hash
        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "com.india.webguru.bizboon", 
                    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));
                System.out.println("Facebook hash===>"+Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (NameNotFoundException e) {
e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
        }

1 个答案:

答案 0 :(得分:0)

通过以下步骤解决了同样的问题:

方法1

转到Cmd:然后C:\Program Files (x86)\Java\jdk1.7.0_02\bin(使用你的路径)

1)生成调试密钥

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

3***W/****************=  (require debug key)

2)生成哈希密钥

keytool -exportcert -alias 3***W/****************= -keystore "C:\Users\Smartphone Developer\.android\debug.keystore" | openssl sha1 -binary | openssl base64

*******+************= (require hash key)

注意:if hash key gives error, then copy paste the same hash generated by FaceBook in exception & add "=" in the end ..

有效哈希FACEBOOK = *************** / a * 0 =

编辑:下载Openssl&放置文件夹,如C:\ OpenSSL

UPDATE 1 (步伐):将Openssl文件夹放入C盘后,打开cmd&写命令:

方法2

第1步

C:\Program Files\Java\jre7>bin

第2步

C:\Program Files\Java\jre7\bin>keytool -exportcert -alias androiddebugkey -keyst
ore "C:\Users\Smartphone Developer\.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha
1 -binary | "C:\OpenSSL\bin\openssl" base64

输入密钥库密码:android

û*** *** ZTŤ*** ***2吨BU * FJ *** W =

第3步

    C:\Program Files\Java\jre7\bin>keytool -exportcert -alias u***ZT***T***2t***bU*Fj***w=
 -keystore "C:\Users\Smartphone Developer\.android\debug.keystore" | "C:\OpenSSL\bin
    \openssl" sha1 -binary | "C:\OpenSSL\bin\openssl" base64

输入密钥库密码:android

d * W + i *** 9 / 6vs *** nP *** CF *** g =(必需的哈希)

如果您下载了正确的openssl,那么它必须正常工作,这是一个经过测试的程序..