Reputation: 3257
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");
}
}
Upvotes: 2
Views: 402
Reputation: 1417
1)You don't need to use 'provider' tag.
2) com.facebook.FacebookActivity is Facebook's own activity, FacebookLoginActivity - yours.
3)Just use this code to get gender, locale, verified, email:
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,gender,verified,locale,email");
request.setParameters(parameters);
request.executeAsync();
4) Could you please share your class. I need more information about "Unexpected call to LoginManager.onActivityResult"
Upvotes: 1
Reputation: 1417
gradle:
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
strings.xml
<string name="facebook_app_id">YOUR_APP_ID</string>
AndroidManifest.xml Add inside of 'application' tag:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
Create this to init FacebookSDK:
public class MainApp extends Application {
@Override
public void onCreate() {
super.onCreate();
FacebookSdk.sdkInitialize(getApplicationContext());
}
}
Add it to AndroidManifest.xml to 'application' tag as the name:
<application
android:name=".MainApp"
In your activity layout add Facebook Login Button:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app.FacebookLoginActivity">
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
</RelativeLayout>
Your FacebookLoginActivity: And Here you can get now your Facebook Token.
public class FacebookLoginActivity extends AppCompatActivity{
private CallbackManager callbackManager;
private LoginButton loginButton;
private String FB_TOKEN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile", "user_friends", "email", "user_birthday");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
/**
* HERE YOU CAN GET YOUR FACEBOOK TOKEN!!!!
**/
FB_TOKEN=loginResult.getAccessToken().getToken();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
If you need to use Custom Facebook Button:
public class FacebookLoginActivity extends AppCompatActivity{
private CallbackManager callbackManager;
private Button loginButton;
private String FB_TOKEN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
callbackManager = CallbackManager.Factory.create();
loginButton = (Button) findViewById(R.id.login_button);
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
/**
* HERE YOU CAN GET YOUR FACEBOOK TOKEN!!!!
**/
FB_TOKEN=loginResult.getAccessToken().getToken();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
Log.e("onError", exception.getMessage());
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(FacebookLoginActivity.this, Arrays.asList("public_profile", "user_friends", "email"));
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
Upvotes: 3