1

I used the following code to Login through Facebook in my app. I need to do this with Facebook SSO. I have the correct app_id.

package com.fb.sso;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class FBSSOActivity extends Activity {
    /** Called when the activity is first created. */


    public static final String APP_ID = "my_app_id";

    private static final String[] PERMISSIONS = new String[] {
            "publish_stream", "read_stream", "offline_access" };
    private TextView mText;
    private Handler mHandler = new Handler();

    private Facebook mFacebook;
    private AsyncFacebookRunner mAsyncRunner;
    byte[] raw;
    private SharedPreferences mPrefs;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (APP_ID == null) {
            Util.showAlert(this, "Warning",
                    "Facebook Applicaton ID must be set...");
        }

        // Initialize the content view
        setContentView(R.layout.main);

        // Initialize the Facebook session
        mFacebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(mFacebook);

        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);
        if (access_token != null) {
            mFacebook.setAccessToken(access_token);
        }
        if (expires != 0) {
            mFacebook.setAccessExpires(expires);
        }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("FB Sample App", "onActivityResult(): " + requestCode);
        mFacebook.authorizeCallback(requestCode, resultCode, data);
    }



    private class LogoutRequestListener implements RequestListener {

        @Override
        public void onComplete(String response, Object state) {
            // TODO Auto-generated method stub

Log.v("comes here>>.","sucess");
            // Only the original owner thread can touch its views
            FBSSOActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    mText.setText("Thanks for using FB Sample App. Bye bye...");

                }
            });

            // Dispatch on its own thread
            mHandler.post(new Runnable() {
                public void run() {
                }
            });

        }

        @Override
        public void onIOException(IOException e, Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
            // TODO Auto-generated method stub
            Log.v("facebook error","fb error");
        }
            }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        MenuItem loginItem = menu.findItem(R.id.login);

        if (mFacebook.isSessionValid()) {
            loginItem.setTitle("Logout");


        } else {
            loginItem.setTitle("Login");


        }
        loginItem.setEnabled(true);
        return super.onPrepareOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        // Login/logout toggle
        case R.id.login:

            if (!mFacebook.isSessionValid()) {

                mFacebook.authorize(this, new DialogListener() {

                    @Override
                    public void onComplete(Bundle values) {
                        // TODO Auto-generated method stub

                        Log.v("Entered ", "No ERRRRRRRRRRRR");
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                mFacebook.getAccessToken());
                        editor.putLong("access_expires",
                                mFacebook.getAccessExpires());
                        editor.commit();
                        Intent i=new Intent(FBSSOActivity.this,second.class);
                        startActivity(i);

                    }

                    @Override
                    public void onFacebookError(FacebookError e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onError(DialogError e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onCancel() {
                        // TODO Auto-generated method stub

                    }

                });
            }else{

                mFacebook.setAccessToken(null);
                mFacebook.setAccessExpires(0);

                AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFacebook);
                 asyncRunner.logout(this.getBaseContext(), new  LogoutRequestListener());
            }

            break;


        default:
            return false;

        }
        return true;
    }

    }

I logged in to preinstalled Facebook app in my device. And I got this screen only. enter image description here

And in my Logcat this line appears.

09-28 15:18:24.652: E/ActivityThread(1201): Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider

If I logged out from my preinstalled Facebook app, my application prompts me to login, asking the credentials. That time also, the empty screen appears.But in the preinstalled Facebook app, I can see my updates.( I get Logged in).

Update:

Right now I'm checking the app only. I have not published it to market. I got the key by referring this site http://sean.lyn.ch/2011/07/android-the-facebook-sdk-sso-and-you/. And I added it to the app, like this. enter image description here

Update 2: Now I got these lines in logcat:

09-29 12:00:12.552: I/ActivityManager(73): Starting activity: Intent { cmp=com.facebook.katana/.ProxyAuth (has extras) }
09-29 12:00:13.022: I/ActivityManager(73): Displayed activity com.facebook.katana/.ProxyAuth: 436 ms (total 436 ms)
09-29 12:00:15.032: W/InputManagerService(73): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@434b2e88 (uid=10031 pid=2233)
09-29 12:00:15.032: W/IInputConnectionWrapper(2233): showStatusIcon on inactive InputConnection
Manikandan
  • 1,479
  • 6
  • 48
  • 89
  • Did you generate a correct and working Hash Key for your app? – Siddharth Lele Sep 28 '12 at 10:40
  • yes. It working fine without sso. I need to implement sso. – Manikandan Sep 28 '12 at 10:44
  • Although you might have the Hash Key configured, it might be wrong. To check it, see the 2nd comment in this answer of mine here: http://stackoverflow.com/a/10516629/450534. It is by far the simplest way of finding the correct Hash Key AFAIK. And most search results for that error suggest the same thing. Incorrect Hash Key. – Siddharth Lele Sep 28 '12 at 10:50
  • I got this line only. 09-28 16:42:19.712: E/ActivityThread(2471): Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider – Manikandan Sep 28 '12 at 11:17
  • I tried the same code in device having os version 1.6 and 2.1. In 1.6 it shows the blank screen. But in 2.1 it shows the login screen. – Manikandan Sep 28 '12 at 11:27
  • @ Siddharth Lele, see the edited question. – Manikandan Sep 28 '12 at 15:59
  • Sorry for the delay. Log on to this site: https://developers.facebook.com/apps/, then select the _Edit Settings_ option and under the _Native Android App_ section you will see the _Key Hash_ textbox to add your Hash Key. – Siddharth Lele Sep 29 '12 at 04:38
  • Yes.. I pasted and still the same thing happens – Manikandan Sep 29 '12 at 04:55
  • And are you checking your logcat using the procedure from the comments in the answer? – Siddharth Lele Sep 29 '12 at 05:13
  • yes.. got this line, "Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider". then the blank screen appears, if it click the back button of the device, it shows "Login cancelled by the user" – Manikandan Sep 29 '12 at 05:23
  • Can you debug through the entire activity and see at what line that happens – Siddharth Lele Sep 29 '12 at 05:24
  • It is not referring any line in the code. In Facebook.java,this line "public static final Uri ATTRIBUTION_ID_CONTENT_URI =Uri.parse("content://com.facebook.katana.provider.AttributionIdProvider"); " has com.facebook.katana.provider.AttributionIdProvider – Manikandan Sep 29 '12 at 05:44
  • can you refer me a working example for facebook sso. – Manikandan Oct 01 '12 at 13:16

0 Answers0