Android Facebook SDK authorize methods shows dialog every time - android

I'm writing an Android application that should upload photos to Facebook.
Everything works just fine except one thing. When I called the Facebook.authorize() method for the first time the Facebook dialog was shown with requested permissions and 'Allow'/'Don't Allow' buttons. That was OK. But when I run my app for the secong time I've got the same dialog, but with message that my application allowed already and suggestion to press OK button.
Is there a way to avoid this second dialog? Should I skip the authorize method in some conditions?
I tried to call the Facebook.isSessionValid() method before authorize method, but this did not help.
Here is my simplified code:
mFacebook = new Facebook(APPLICATION_ID);
mFacebookAsync = new AsyncFacebookRunner(mFacebook);
if (mFacebook.isSessionValid()) {
uploadPictureFile();
}
else {
mFacebook.authorize(this, new String[] {"publish_stream"}, new Facebook.DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
Toast.makeText(PhotoFeederFacebookSendActivity.this, "Facebook error: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
finishWithResultCode(RESULT_CANCELED);
}
#Override
public void onError(DialogError e) {
Toast.makeText(PhotoFeederFacebookSendActivity.this, "Facebook dialog error: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
finishWithResultCode(RESULT_CANCELED);
}
#Override
public void onComplete(Bundle values) {
uploadPictureFile();
}
#Override
public void onCancel() {Toast.makeText(PhotoFeederFacebookSendActivity.this, "Facebook authorization cancelled.", Toast.LENGTH_LONG).show();
finishWithResultCode(RESULT_CANCELED);
}
});
}
And here is my onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
Any help appreciated. Thanks.

You need to call mFacebook.setAccessToken("your token") before mFacebook.isSessionValid()
and you need to save the token to preference yourself

If you only intend to update status see my post here...
Android Facebook Graph API to update status

Related

Check shared Post on Facebook Successfully or discarded

Facebook SDK always call onSuccess(Sharer.Result result) after discarding the post.
I am using the share post of the Facebook SDK, but always call onSuccess listener even after the discard or cancel the post.
I am using this facebook SDK for share the post implementation 'com.facebook.android:facebook-share:[4,5)'and use this doc.
https://developers.facebook.com/docs/sharing/android/
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.e(TAG, result.toString());
Toast.makeText(MainActivity.this, "shared succrssfull", Toast.LENGTH_SHORT).show();
}
#Override
public void onCancel() {
Toast.makeText(MainActivity.this, "shared cancel", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException error) {
Log.e(TAG, error.toString());
Toast.makeText(MainActivity.this, "shared error occured", Toast.LENGTH_SHORT).show();
}
});
and have onActivity Result is
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
but always called onSuccess listner , how can I find the post has been succesfully post.
have checked with multiple link but not got any solution yet so please help me out.
Post Id facebook share dialog always return null in Android
FB share dialog cancel callback not working in Android
we can not recognize the post has been successful or not because the policy has been given by Facebook.
Don't incentivize people to post content on Facebook or give the impression that posting to Facebook will be rewarded. For example, don’t give people anything in exchange for them posting to Facebook (i.e. don’t give or promise virtual goods, achievements, coupons, discounts, access to content or extra entries in a promotion, if they post content to Facebook).
the policy of Facebook has been given below.
https://developers.facebook.com/docs/apps/examples-platform-policy-4.5
https://developers.facebook.com/policy#properuse

How to Retrieve Login Callback from twitter webview using fabric

I am implementing twitter login using fabric, the login with native app seems fine. But when i am trying the login without twitter app installed in the device, it starts authorization with the webview, but on completion i didnt receive any callback neither in onSucccess or failure method
mTwitterAuthClient.authorize(ConnectSocialMedia.this, new Callback<com.twitter.sdk.android.core.TwitterSession>() {
#Override
public void success(Result<com.twitter.sdk.android.core.TwitterSession> result) {
// Success
mDialog.dismiss();
// The TwitterSession is also available through:
// Twitter.getInstance().core.getSessionManager().getActiveSession()
com.twitter.sdk.android.core.TwitterSession session = result.data;
ApplicationData.setSharedPrefValue(mActivity, "twittertoken", session.getAuthToken().token);
ApplicationData.setSharedPrefValue(mActivity, "twittertokensecret", session.getAuthToken().secret);
ApplicationData.setSharedPrefValue(mActivity, "twitterid", String.valueOf(session.getUserId()));
imgTwitter.setVisibility(View.VISIBLE);
mTwitterLoginButton.setEnabled(false);
btnClose.setVisibility(View.VISIBLE);
// getTwitterCover(String.valueOf(session.getUserId()));
}
#Override
public void failure(TwitterException e) {
mDialog.dismiss();
Log.d("TwitterKit", "Login with Twitter failure", e);
Toast.makeText(mActivity, "Twitter Login Failed", Toast.LENGTH_SHORT).show();
mTwitterLoginButton.setEnabled(true);
}
});
}
});
and neither on onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Make sure that the mTwitterLoginButton hears the result from any
// Activity that it triggered.
mTwitterAuthClient.onActivityResult(requestCode, resultCode, data);
}
Please help me, thanks
Add any call back Url in your Twitter app settings page. Every thing will work correctly after that. :)

Android facebook sdk login error 1118578

Everything was working fine, until I wanted to check what happens if the user clicks the Login with Facebook button with no internet connection available.
A toast showed up saying:
Login failed. Please contact the maker of this app and ask them to report issue #1118578 to Facebook.
In the logcat I have:
D/Facebook-authorize(2824): Login canceled by user.
I tried to get rid of this toast and display my own error message in the onCancel() method - but I can't find a way to disable that toast.
When I enabled WiFi again, single sign on didn't work anymore!
What could be the cause of this?
private void fbLogin() {
facebook.authorize(this, new String[] { "email" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
app.save("fb_token", facebook.getAccessToken());
app.save("fb_expire", facebook.getAccessExpires());
mAsyncRunner.request("me", new meRequestListener());
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
EDIT:
To answer my own question, removing android:launchMode="singleInstance" From the manifest in the <activity> resolved the #1118578 issue.
However, I do need the android:launchMode="singleInstance" for the twitter OAuth login, as the browser passes the data back to the activity via new Intent. If I do not provide android:launchMode="singleInstance" in the manifest, a new activity is launched and it does not recieve the OAuth verifier.
Is there any way around this, to use Facebook and Twitter login in the same activity?
The only solution I think of is to use a dummy activity for Twitter4j with singleInstance.
My way to overcome this has been by changing the callback in twitter, from "twitter://callback" to "http://localhost/callback" and remove the android:launchMode="singleInstance" and the twitter intent-filter from the manifest.
I can still get the response in the WebView (rather than on the onResume() as before):
yourwebview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// your_callback == http://localhost/callback
if(url == null || !url.startsWith(your_callback))
return;
Uri uri = Uri.parse(url);
String ov = uri.getQueryParameter("oauth_verifier");
AccessToken at = null;
...

Android Facebook sdk can't handle device rotation properly?

I'd like to authenticate my users using the Facebook SSO service. I managed to handle basic login with the official Android Facebook sdk, which was quite painless, but it seems that the sdk just can't handle device rotation - at least I couldn't find a way to get it work correctly.
The auth code:
loginButtonFacebook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do FB login
facebook.authorize(LoginActivity.this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
Toast.makeText(LoginActivity.this, "Facebook login successful", Toast.LENGTH_SHORT).show();
Log.d(TAG, "FACEBOOK LOGIN SUCCESSFUL");
//loginFB(facebook.getAccessToken());
}
#Override
public void onFacebookError(FacebookError error) {
Toast.makeText(LoginActivity.this, "FacebookError:" +error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onError(DialogError e) {
Toast.makeText(LoginActivity.this, "Error: "+e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCancel() {
Toast.makeText(LoginActivity.this, "Cancelled!", Toast.LENGTH_SHORT).show();
}
});
}
});
My Activity also needs to handle the result from the FB login activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
This works, but the facebook.authorize(...) method need a reference for my current Activity, so it can do a callback later. But if the device is rotated when the user is in the facebook authorization Activity, my previous Activity gets destroyed. So after the user authorizes my app., I don't get the callback about this.
Has anyone found a solution for this?
Thanks
in your manifest file under your activity name just add the following line
android:configChanges="orientation"
inside your activity
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
................
}
Refer here and here
I usually hate recommending this, but the Activity that Facebook uses can be locked to portrait without any negative consequences. All it does is show an indefinite ProgressBar.
Check your manifest for the activity with the name com.facebook.LoginActivity and make sure it has the following properties:
<activity
android:configChanges="keyboardHidden"
android:name="com.facebook.LoginActivity"
android:screenOrientation="portrait" />

Unable to share Content on Facebook through Android App

I have an android app in which i am using Facebook Android SDK to post content from my application to Facebook Wall. My code was working perfectly till i updated the Facebook app on my phone. Since that time, whenever i try to post content on Facebook, i get the login screen with the "Loading" option and after a few seconds, nothing happens. Here is the code which i am using
On Facebook Button CLick
if(isOnline())
{
mFacebook = new Facebook("APP ID");
mFacebook.authorize(this,new String[] {"publish_stream", "read_stream", "offline_access"}, new AuthorizeListener());
}
The Code for Authorize listener is
class AuthorizeListener implements DialogListener {
public void onComplete(Bundle values) {
// Handle a successful login
postOnWall("My wall text");
}
#Override
public void onCancel() {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onFacebookError(FacebookError e) {
}
}
After debugging i found that the OnComplete method is not being called after calling the authorize function and there is also no exception. This was working perfectly till i update the Facebook app on my Android phone.
Any help will be appreciated
Make sure you are calling Facebook.authorizeCallback in your Activity's onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
// ... anything else your app does onActivityResult ...
}
For the details, carefully read this page.

Categories

Resources