I would like to get an Access Token for accessing the Graph for my Application, without using the Facebook.Login button. (The button is actually placed in the Action Bar).
Thanks in advance!
In onSuccess from LoginResult you can get AccessToken).
if (callbackManager == null) {
callbackManager = CallbackManager.Factory.create();
}
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken.setCurrentAccessToken(loginResult.getAccessToken());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
Related
I want to ask the user of my android app the permission to publish on facebook when a custom share button (it's just an ImageView) is pressed. On button's OnClick method I execute this block:
CallbackManager facebookCallbackManager;
...
facebookCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(facebookCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
shareContent(activity, content, true);
}
#Override
public void onCancel() { }
#Override
public void onError(FacebookException error) { }
});
LoginManager.getInstance().logInWithPublishPermissions(activity, Collections.singletonList("publish_actions"));
And then I override:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebookCallbackManager.onActivityResult(requestCode, resultCode, data);
}
The problem is that the request never comes: an everlasting spinner wheel is presented and no callback (nor success, nor cancel, nor error) is called.
a. The user is already logged in, according to:
public static boolean isLoggedIn() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null;
}
b. FacebookSdk.isInitialized() is true
c. Publish permissions are not granted, according to:
private static boolean hasPublishPermissions() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null && accessToken.getPermissions().contains("publish_actions");
}
d. Other uses of FB SDK are used through the app and they are functioning.
e. I am an app admin on FB dashboard
Any idea on the problem?
Important PS:
Since Facebook's API is extremely stable, depending on the time of the day or the position of the stars, without changing code I have three possible outcomes:
As described before, an everlasting spinner wheel.
The callback fires the onCancel method without the user interaction.
It shares the content without asking for confirmation - this gave me a nice unwanted video posted on my personal FB, without me noticing :) -
PS2:
Even the classic LoginManager.getInstance().logInWithReadPermissionnow is having the same issue. It never had it before.
then you can share content without everlasting spinner
LoginManager.getInstance().registerCallback(mCallbackManagerFacebook, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("Success");
if (loginResult.getRecentlyGrantedPermissions().contains("publish_actions")) {
new shareContent(activity, content, true);
} else {
//Toast.makeText(youractivity.this, "Successfully Connected to Facebook.", Toast.LENGTH_LONG).show();
GetFacebookPublishPermissions();
}
}
#Override
public void onCancel() {
//Log.d(TAG_CANCEL, "On cancel");
LoginManager.getInstance().logOut();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(youractivity.this, "Looks like we are unable to connect to Facebook Servers.", Toast.LENGTH_LONG).show();
LoginManager.getInstance().logOut();
// Log.d(TAG_ERROR, error.toString());
}
});
and getFacebookSharepermission
public void GetFacebookPublishPermissions(){
if(LoginManager.getInstance()!=null)
{
LoginManager.getInstance().logInWithPublishPermissions(youractivity.this, Arrays.asList("publish_actions"));
}
}
Update
you can call this function like below
AccessToken token = com.facebook.AccessToken.getCurrentAccessToken();
if(token!=null)
{
if (token.getPermissions().contains("publish_actions")) {
new shareContent(activity, content, true);
}else{
GetFacebookPublishPermissions();
}
}else{
LoginManager.getInstance().logInWithReadPermissions(this,
Arrays.asList("public_profile", "email", "user_birthday"));
}
I think there is mistake for callback manager.
you declare and assign CallbackManager facebookCallbackManager;
facebookCallbackManager = CallbackManager.Factory.create();
Here you can see there is cbManager instead of facebookCallbackManager
so please double check for that.
V
LoginManager.getInstance().registerCallback(cbManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
shareContent(activity, content, true);
}
#Override
public void onCancel() { }
#Override
public void onError(FacebookException error) { }
});
I am using LoginButton of facebook widget to login through facebook.
mLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this,Arrays.asList("public_profile", "user_friends"));
}
});
I am having two issues-
1.When I click on login button facebook login dialog opens two times.So after I logged in for first time after providing user email and password it shows another login dialog behind which i have to close.
2.After I logged in and move to next screen,if i come back to login screen it shows logout text in Login button which is ideal.But when i click on it it open both login and logout dialog box.Ideally it should open just logout dialog box.How should i prevent LoginManager.getInstance().logInWithReadPermissions(MainActivity.this,Arrays.asList("public_profile", "user_friends"));from being executed if user has already logged in.
Below is he code I am using
public class MainActivity extends AppCompatActivity {
private LoginButton mLoginButton;
CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
mLoginButton = (LoginButton) findViewById(R.id.login_button);
mLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if(accessToken==null) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "user_friends"));
}
}
});
mLoginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException exception) {
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
Remove setOnClickListener on loginButton, just RegisterCallBack on Login Button and it will handle it all, you don't need setOnClickListener for loginButton. It is called twice because you have set setOnClickListener and registerCalBack both.
CallbackManager callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.fbLogin);
loginButton.setReadPermissions(Arrays.asList("user_status,public_profile,user_friends,email,user_birthday"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
You can check if the user is logged in or not by
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken != null) {
}
return accessToken != null;
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_register_rs1);
//String username = getIntent().getExtras().getString("username");
//Log.w("username is",username);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.d("FACEBOOK", "SUCCESS");
}
#Override
public void onCancel() {
// App code
Log.d("FACEBOOK", "CANCEL");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.d("FACEBOOK", "ERROR"+exception.toString());
}
});
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.d("FACEBOOK MANAGER", "SUCCESS");
}
#Override
public void onCancel() {
// App code
Log.d("FACEBOOK MANAGER", "CANCEL");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.d("FACEBOOK MANAGER", "ERROR");
}
});
i use this code to Login with facebook and when i press to the facebook login button .. it's just stay loading and the FacebookActivity never show..
this is my logcat screen:
W/BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 8328
I opened an issue with Facebook for this problem - or at least what I think is the same issue.. basically in my case the login process is very slow and seems to hang for some users/devices.
If anyone has a similar issue can you add additional details to the bug below so we can get someone at FB to investigate?
https://developers.facebook.com/bugs/264734493940722/
I found Facebook Android SDK has deprecated its class Session in its 4.x version. What class or method I should use to replace Session.OpenRequest? Thanks.
The classes you need to look into are CallbackManager and LoginManager (and also AccessToken). I replaced my old Session.OpenRequest code with something akin to the following, it may be helpful as a starting point:
FacebookSdk.sdkInitialize(getApplicationContext());
m_CallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(m_CallbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
completeLogin();
}
#Override
public void onCancel() {
// TODO: this
}
#Override
public void onError(FacebookException exception) {
// TODO: this
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
I want to login Facebook SDK 4.3 with my own button but I'm facing some problems. Here is my code
public class MainActivity extends Activity {
CallbackManager callbackManager;
ShareDialog shareDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
Button bt= (Button)findViewById(R.id.button1);
shareDialog = new ShareDialog(this);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openFB();
}
});
}
public void openFB() {
//Login Callback registration
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(), "in LoginResult on success", Toast.LENGTH_LONG).show();
//Login success - process to Post
if (ShareDialog.canShow(ShareLinkContent.class)) {
String description = "description";
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("title")
.setContentDescription(description)
.setContentUrl(Uri.parse("http://google.com"))
.setImageUrl(Uri.parse("http://google.com"))
.build();
shareDialog.show(linkContent, ShareDialog.Mode.FEED);
}
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "in LoginResult on cancel", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), "in LoginResult on error", Toast.LENGTH_LONG).show();
}
});
LoginManager.getInstance().logInWithReadPermissions(this, "user_friends"); //Log in to FB
}
}
I get this error at the last line:
The method logInWithReadPermissions(Fragment, Collection) in
the type LoginManager is not applicable for the arguments
(MainActivity, String)
As you know there is another logInWithReadPermissions method with 2 parameters are (Activity,Collection) but I don't know I get this error. Please help me to fix this!
For someone who encounter the same problem with me, just change the last line like this:
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends")); //Log in to FB
and it should be fine.