Android Facebook Single Sign On error
Asked Answered
M

1

0

I am trying to implement Facebook SSO in my android app.It opens native Facebook app, after login process complete it shows "500: internal server error".What I am doing wrong?

Any suggestion will be highly appreciated . Here is my code

public class FacebookUtil {

    public static Facebook facebook = new Facebook(AppConstants.FACEBOOK_APP_ID);
    String FILENAME = "AndroidSSO_data";
    private static SharedPreferences mPrefs;
    private static Context mContext;
    private static  boolean isSucess;
    private static FacebookSucessListener mFacebookSucessListener;
     public static final String FB_APP_SIGNATURE ="xxxxxxxxxxxxxxxxxxx"; 
    public interface FacebookSucessListener {
        public void onSucess(boolean isSucess);
    }
    public static void postToWall(){
        facebook.dialog(mContext, "feed" , new DialogListener(){
            public void onCancel() {

            }
            public void onComplete(Bundle arg0) {

            }

            public void onError(DialogError arg0) {

            }

            public void onFacebookError(FacebookError arg0) {

            }});
    }

    /*
     * Function to update status
     * @param facebook acesstoken acess token
     * @param  String message to post
     */
    public static void updateStatus(String accessToken, final String aMsgToShare){
        try {
            Bundle bundle = new Bundle();
            bundle.putString("message", aMsgToShare);
            bundle.putString(Facebook.TOKEN, accessToken);
            String response = facebook.request("me/feed",bundle,"POST");
            if(response.contains("Duplicate status message"))
                Toast.makeText(mContext, "Duplicate message can not post", Toast.LENGTH_SHORT).show();
            else if (response.contains("The user hasn't authorized the application to perform this action"))
                Toast.makeText(mContext, "The user hasn't authorized the application to perform this action",Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(mContext,"Posted Successfuly", Toast.LENGTH_SHORT).show();
        } catch (MalformedURLException e) {
            Log.e("MALFORMED URL",""+e.getMessage());

        } catch (IOException e) {

        }
    }
    /*
     * To logout from facebook
     * @param Activity activity
     * return response
     */
    public static String logoutFacebook(Activity aActivity){
        String resonse = "";
        try {
            String response = facebook.logout(aActivity.getApplicationContext());
        } catch (Exception e) {
            Log.e("LogoutException",""+e.getMessage());
        }
        return resonse;
    }


    /*
     * TO start the  facebook login process
     * @param Activity activity
     * @param SharedPreferences pref
     * return true in case of sucess false otherwise
     */
    public static void startLoginProcess(final Activity aActivity,final SharedPreferences mPrefs,FacebookSucessListener facebookSucessListener)
    {
        try{
            mFacebookSucessListener = facebookSucessListener;
        }catch(Exception e){}

        /*
         * Get existing access_token if any
         */
        mContext=aActivity;
        String access_token = mPrefs.getString("fb_access_token", null);
        long expires = mPrefs.getLong("fb_expire_time", 0);
        if(access_token != null) {
            facebook.setAccessToken(access_token);
        }
        if(expires != 0) {
            facebook.setAccessExpires(expires);
        }
        boolean isSessionValid  = facebook.isSessionValid();
        if(!isSessionValid)
        {
            //Facebook.FORCE_DIALOG_AUTH
            facebook.authorize( aActivity, AppConstants.permissions, 1, new DialogListener() {
                public void onComplete(Bundle values) {
                    String token = values.getString(Facebook.TOKEN);
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("fb_access_token", facebook.getAccessToken());
                    editor.putLong("fb_expire_time", facebook.getAccessExpires());
                    editor.putBoolean("is_fb_logged_in", true);
                    editor.commit();
                    mFacebookSucessListener.onSucess(true);
                }

                public void onFacebookError(FacebookError error) {
                    mFacebookSucessListener.onSucess(false);
                    Log.e("Facebook-onFacebookError", error.getMessage());
                    Toast.makeText(aActivity, "Error: "+ error.getMessage(), 600).show();
                }
                public void onError(DialogError e) {
                    mFacebookSucessListener.onSucess(false);
                    Log.e("Facebook-onError", e.getMessage());
                    Toast.makeText(aActivity, "Error: "+ e.getMessage(), 600).show();

                }

                public void onCancel() {

                }
            });
        }
        else {
            mFacebookSucessListener.onSucess(true);
        }


    }
    final class PostDialogListener implements DialogListener {
        public void onComplete(Bundle values) {

        }

        public void onFacebookError(FacebookError error) {
            Log.e("Facebook-onFacebookError", error.getMessage());
        }

        public void onError(DialogError error) {
            Log.e("Facebook-onFacebookError", error.getMessage());
        }

        public void onCancel() {

        }
    }


}
Matt answered 30/9, 2012 at 11:41 Comment(1)
means something is wrong on the server side. can you post your code?Belaud
M
5

Finally I solved my problem.The Has key, I was generating through CMD in window 7, was wrong. So I generated this programmatically.

private void getHashKey()
{
        PackageInfo info;
        try {
        info = getPackageManager().getPackageInfo("your app package name", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
                   MessageDigest md;
        md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
                   //String something = new String(Base64.encode(md.digest(), 0));
                     String something = new String(Base64.encode(md.digest(),0));
                   Log.e("**** Hash Key", something);
        } 
        }
        catch (NameNotFoundException e1) {
        Log.e("name not found", e1.toString());
        }

        catch (NoSuchAlgorithmException e) {
        Log.e("no such an algorithm", e.toString());
        }
        catch (Exception e){
        Log.e("exception", e.toString());
        }

}
Matt answered 15/10, 2012 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.