Facebook Login and Post To Wall works for first time only

夙愿已清 提交于 2019-12-09 13:56:07

问题


I have a strange situation.I am trying to integrate facebook with my app.But the login and post to wall works only for the first time.I am struck for the past 2 days and could not find a solution.

Here is the code snippet:

//.............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

                                    }

                                });
                    }



                }

I am inflating a customize view to post on the wall.For the first time the value of access_token is null and expires is zero.But when i try to share for the second time nothing happens.I debugged my application and find that now there is some value in access_token as well as expires and the condition if (!facebook.isSessionValid()) is not firing.Any help??I know I am using some deprecated method but I couldn't find updated tutorials or documentations.

Here is a code of post to wall:

   /**
     * 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() {
      }
     });

    }

Update:

I am using this code to generate hash:

 // 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:


Same issue was solved by following steps:

METHOD 1

Goto Cmd : then C:\Program Files (x86)\Java\jdk1.7.0_02\bin (use your path)

1) Generate Debug key

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

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

2) Generate Hash Key

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

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

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

VALID HASH FOR FACEBOOK = ***************/a*0=

Edit: Download Openssl & place the folder like C:\OpenSSL

UPDATE 1 (step vise): After placing Openssl folder in C drive, open cmd & write commands:

METHOD 2

step 1

C:\Program Files\Java\jre7>bin

step 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

Enter keystore password: android

u***ZT***T***2t***bU*Fj***w=

step 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

Enter keystore password: android

d*W+i***9/6vs***nP***CF***g= (Required Hash)

if you downloaded the correct openssl then it must work, its a tested procedure..



来源:https://stackoverflow.com/questions/25356984/facebook-login-and-post-to-wall-works-for-first-time-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!