I'm using this. In my Facebook.java
in my authorized()
method I call startSingleSignOn
and startDialogAuth
, I'm using the DEFAULT_AUTH_ACTIVITY_CODE
instead of FORCE_DIALOG_AUTH
public void authorize(Activity activity, String[] permissions, int activityCode, final DialogListener listener) {
boolean singleSignOnStarted = false;
mAuthDialogListener = listener;
// Prefer single sign-on, where available.
if (activityCode >= 0) {
singleSignOnStarted = startSingleSignOn(activity, mAppId,
permissions, activityCode);
}
// Otherwise fall back to traditional dialog.
if (!singleSignOnStarted) {
startDialogAuth(activity, permissions);
}
}
I noticed that in my startDialogAuth
it sets the AccessToken
and AccessExpires
private void startDialogAuth(Activity activity, String[] permissions) {
Bundle params = new Bundle();
if (permissions.length > 0) {
params.putString("scope", TextUtils.join(",", permissions));
}
CookieSyncManager.createInstance(activity);
dialog(activity, LOGIN, params, new DialogListener() {
public void onComplete(Bundle values) {
// ensure any cookies set by the dialog are saved
CookieSyncManager.getInstance().sync();
setAccessToken(values.getString(TOKEN));
setAccessExpiresIn(values.getString(EXPIRES));
if (isSessionValid()) {
Util.logd("Facebook-authorize", "Login Success! access_token="
+ getAccessToken() + " expires="
+ getAccessExpires());
mAuthDialogListener.onComplete(values);
} else {
mAuthDialogListener.onFacebookError(new FacebookError(
"Failed to receive access token."));
}
}
public void onError(DialogError error) {
Util.logd("Facebook-authorize", "Login failed: " + error);
mAuthDialogListener.onError(error);
}
public void onFacebookError(FacebookError error) {
Util.logd("Facebook-authorize", "Login failed: " + error);
mAuthDialogListener.onFacebookError(error);
}
public void onCancel() {
Util.logd("Facebook-authorize", "Login canceled");
mAuthDialogListener.onCancel();
}
});
}
And in the startSingleSignOn
it looks like this
private boolean startSingleSignOn(Activity activity, String applicationId,
String[] permissions, int activityCode) {
boolean didSucceed = true;
Intent intent = new Intent();
intent.setClassName("com.facebook.katana",
"com.facebook.katana.ProxyAuth");
intent.putExtra("client_id", applicationId);
if (permissions.length > 0) {
intent.putExtra("scope", TextUtils.join(",", permissions));
}
// Verify that the application whose package name is
// com.facebook.katana.ProxyAuth
// has the expected FB app signature.
if (!validateActivityIntent(activity, intent)) {
return false;
}
mAuthActivity = activity;
mAuthPermissions = permissions;
mAuthActivityCode = activityCode;
try {
activity.startActivityForResult(intent, activityCode);
} catch (ActivityNotFoundException e) {
didSucceed = false;
}
return didSucceed;
}
I'm wondering where it sets the Access Token
that is needed when logs-in, I'm facing the problem where the user has a Facebook app
installed and already authorized my app, it will only show a dialog (Loading) and display a white screen then dismiss but nothing happen next. It doesn't get the user's information from the logged Facebook app
. But when the user doesn't have a Facebook app
it displays the WebView and just working fine, it logs in correctly. While debugging it I also notice that it doesn't go inside my function LoginDialogListener
here it implements Facebook.DialogListener
(when there is facebook app installed) maybe because in startSingleSignOn
it doesn't call CookieSyncManager.createInstance(activity);
dialog(activity, LOGIN, params, new DialogListener() {
here's my activity
code where it calls the facebook.authorize
public void setFacebookConnection() {
facebook = new Facebook(Constants.FACEBOOK_APP_ID);
facebookAsyncRunner = new AsyncFacebookRunner(facebook);
facebook.authorize(this, Constants.FACEBOOK_PERMISSIONS, new LoginDialogListener());
}
private class LoginDialogListener implements Facebook.DialogListener {
public void onComplete(Bundle values) {
Log.d(TAG, "LoginONComplete");
String token = facebook.getAccessToken();
long token_expires = facebook.getAccessExpires();
Log.d(TAG, "AccessToken: " + token);
Log.d(TAG, "AccessExpires: " + token_expires);
if (isPublishStreamAuthorized()) {
facebookSharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
facebookSharedPreferences
.edit()
.putLong(Constants.FACEBOOK_ACCESS_EXPIRES,
token_expires).commit();
facebookSharedPreferences.edit()
.putString(Constants.FACEBOOK_ACCESS_TOKEN, token)
.commit();
facebookAsyncRunner.request("me", new IDRequestListener());
} else {
logoutFacebook();
}
}
public void onFacebookError(FacebookError e) {
Log.d(TAG, "FacebookError: " + e.getMessage());
}
public void onError(DialogError e) {
Log.d(TAG, "Error: " + e.getMessage());
Toast.makeText(getApplicationContext(), Constants.NO_INTERNET_CONNECTION,
Toast.LENGTH_LONG).show();
}
public void onCancel() {
Log.d(TAG, "OnCancel");
logoutFacebook();
}
}
It seems that my facebook.getAccessToken();
in my activity returns null, as tried calling in my setFacebookConnection()
after calling facebook.authorize()
Can you please help me? I needed to solve this soon. Thank you so much.
Update
I found this and tried doing step 3 but it ends up opening the WebView
again.
***Update
I finally get the access token
, by calling onActivityResult()
but the get data will only show after I close the app and open it again here is my code where FACEBOOK_AUTH_RESULT_CODE
value I changed it to 32665.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.FACEBOOK_AUTH_RESULT_CODE) {
facebook.authorizeCallback(requestCode, resultCode, data);
} else {
Log.e(TAG, "onActivityResult Error : Authentication Error");
}
}