Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to retrieve access token for facebook on real device

I did Facebook integration in my project, everything is fine on emulator. When it comes to run on real device it is not working. I think the problem is Facebook access token, I don't know what to do now? So I am unable to retrieve friends information on real device. Can anybody help me how to get access token on real device.

I am using Android SDK only to get the Facebook friends information.

mFacebook = new Facebook("api_id");
mFacebook.authorize(this, new String[] {
    "publish_stream", "read_stream", "offline_access", "friends_birthday", "user_birthday", "email", "read_friendlists", "manage_friendlists"
}, this);
sToken = mFacebook.getAccessToken();
public void onComplete(Bundle values) {
    Log.e("oncomplete", "value");
    if (values.isEmpty()) {
        Log.e("oncomplete", "value is empty");
        return;
    }
    if (!values.containsKey("POST")) {
        sToken = mFacebook.getAccessToken();
        getFriends()
    }
}
private void getFriends() {
    try {
        sToken = mFacebook.getAccessToken();
        StaticUtils.sResponseId = mFacebook.request("me/friends");
        Log.w("response", StaticUtils.sResponseId);
        try {
            JSONObject jObj = Util.parseJson(StaticUtils.sResponseId);
            JSONArray jArr = jObj.getJSONArray("data");
            for (int i = 0; i < jArr.length(); i++) {
                JSONObject jObjFren = jArr.getJSONObject(i);
                Iterator it = jObjFren.keys();
                while (it.hasNext()) {
                    String s = (String) it.next();
                    // Log.w("KEY",s);
                    String sname = jObjFren.getString(s);
                    if (s.equals("id")) {
                        StaticUtils.sFbId.add(sname);
                        StaticUtils.sFbPics.add(StaticUtils.sImgUrl + sname + "/picture");
                    } else if (s.equals("name")) {
                        StaticUtils.sFbName.add(sname.toLowerCase());
                    }
                }
            }
        } catch (JSONException e) {
            Log.w("json exception", e.toString());
        } catch (FacebookError e) {
            Log.w("facebook exception", e.toString());
        }
    } catch (MalformedURLException e) {
        Log.w("malformed exception", e.toString());
    } catch (IOException e) {
        Log.w("io exception", e.toString());
    }
}  

Thanks, Ammu

like image 320
Taruni Avatar asked May 07 '11 05:05

Taruni


People also ask

Why does Facebook not recognize my device?

the main reason is that fb might have detected unauthorized access to your account therefore to verify your account to avoid unauthorized access. If you have cleared your cookies of your recognized device then write to support team they will provide you alternative verification method. Erlis D.


2 Answers

The code above given is absolutely correct...The problem is if we have already installed any facebook application in our device which are upgraded versions than we are using our application will not work...If we uninstall the facebook applications in our device it will work...and there is also another way to work our application and other facebook application in our device by following steps::: if u r using eclipse then goto

Windows>Preferences>Android>Build and get the path of the debug keystore

now open terminal and run the following command

keytool -export -alias androiddebugkey -keystore "debug keystore path" | openssl sha1 -binary | openssl enc -a -e

it will ask for the password then enter android as ur password Now add the HASH KEY to your Facebook API configuration located @ http://www.facebook.com/developers/ under EDIT SETTINGS / Mobile and Devices screen.

Now i was able to run my application on HTC device

like image 195
Taruni Avatar answered Oct 25 '22 17:10

Taruni


There is another answer for this question. We can have pre-installed facebook application to our mobile device , but we will not be interacting with the already installed application,i.e. we will not use single sign on ,better known as SSO(though it is recommended to use). We are having this method in facebook sdk. single sign-on may be disabled by passing FORCE_DIALOG_AUTH as the activityCode parameter in your call to authorize().

public void authorize(Activity activity, String[] permissions,
        int activityCode, final DialogListener listener)

Here the third parameter plays the role i.e. activityCode.Now lets see its role

 if (activityCode >= 0) {
        singleSignOnStarted = startSingleSignOn(activity, mAppId,
                permissions, activityCode);
    }
    // Otherwise fall back to traditional dialog.
    if (!singleSignOnStarted) {
        startDialogAuth(activity, permissions);
    }

It means if we want to be untouched with the pre installed facebook application we have to pass activityCode with less than 0 (< 0) value.Now in your main activity where facebook api is called use

int FORCE_DIALOG_AUTH =-1;
mFacebook.authorize(this, new String[] { "publish_stream",
        "read_stream", "offline_access", "friends_birthday",
        "user_birthday", "email", "read_friendlists",
        "manage_friendlists" },FORCE_DIALOG_AUTH , new LoginDialogListener());

If we would like to force the use of legacy dialog-based authorization, pass FORCE_DIALOG_AUTH for this parameter. Otherwise just omit this parameter and Facebook will use a suitable default. It was just to solve the problem from SSO.

like image 37
Shahzad Imam Avatar answered Oct 25 '22 17:10

Shahzad Imam