android Facebook SDK cannot share image - android

I am developing an app that can share an image to Facebook.
Only I can share image, other people cannot share. I have a FAB and calling this activity on its onClick() method. I really don't know what to do, my app is live but when I click on my app name over the shared photo I get
"Misconfigured App
Sorry, MyApp hasn't been approved for display in App Centre."
What is the reason that others cannot share images? I really need the answer. Thanks.
public class ShareActivity extends AppCompatActivity {
private CallbackManager callbackManager;
private LoginManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
List<String> permissionNeeds = Arrays.asList("publish_actions");
//this loginManager helps you eliminate adding a LoginButton to your UI
manager = LoginManager.getInstance();
manager.logInWithPublishPermissions(this, permissionNeeds);
manager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
sharePhotoToFacebook();
Toast.makeText(ShareActivity.this, "Success", Toast.LENGTH_LONG).show();
finish();
}
#Override
public void onCancel() {
Toast.makeText(ShareActivity.this, "Cancel", Toast.LENGTH_LONG).show();
finish();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(ShareActivity.this, "Error", Toast.LENGTH_LONG).show();
finish();
}
});
}
}
private void sharePhotoToFacebook(){
try {
URL url = new URL("http://burakakyalcin.site/image.png");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(image)
.setCaption("Hello")
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
ShareApi.share(content, null);
} catch(IOException e) {
System.out.println(e);
}
}
#Override
protected void onActivityResult(int requestCode, int responseCode, Intent data)
{
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}
}

Related

How do I use SharedPreferences with Facebook SDK?

Currently this is how my FB login looks like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
TAG = "Login.Activity";
//Callback manager manages callbacks into the FB SDK from an Activity's onActivityResult() Method.
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
//If login in successful,
#Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
goMainScreen(profile);
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(),R.string.cancel_login, Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(getApplicationContext(), R.string.error_login, Toast.LENGTH_LONG).show();
}
});
}
private void goMainScreen(Profile profile) {
if(profile != null){
//Passing in the name,id and photo from the profile.
Intent intent = new Intent(this, MainActivity.class);
// intent.putExtra("name",profile.getName());
intent.putExtra("id",profile.getId());
intent.putExtra("photo",profile.getProfilePictureUri(200,200));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
#Override
//All Request Code, Result Code, and data are recieved by the activity
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}
}
I would like to keep track of the user session throughout other activities and know if they have logged in before.Would I use sharedpreferences for this? If so will I be doing this at the beginning of the loginActivity?
Well you could use what provide the SDK and create a method like this one :
public boolean isLoggedIn() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null;
}

Dismiss facebook login dialog android

I've integrated facebook sdk v-4 with my android app. Here I've worked facebook login in 2 activities.
1) In one activity fb login is done via LoginButton. Where after successful login the fb login dialog is dismissed by facebook sdk automatically.
code:
fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
ProfileTracker profileTracker;
#Override
public void onSuccess(LoginResult loginResult) {
Logger.d("FBLOGIN", "login successful. User ID: "
+ loginResult.getAccessToken().getUserId()
+ "\n" +
"Auth Token: "
+ loginResult.getAccessToken().getToken());
com.facebook.Profile fbProfile = com.facebook.Profile.getCurrentProfile();
if (fbProfile == null) {
profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(com.facebook.Profile oldProfile, com.facebook.Profile currentProfile) {
profileTracker.stopTracking();
doTaskWithFbProfile(currentProfile);
}
};
profileTracker.startTracking();
} else {
doTaskWithFbProfile(fbProfile);
}
}
#Override
public void onCancel() {
Logger.d(TAG, "fb Login attempt canceled.");
}
#Override
public void onError(FacebookException e) {
Logger.e(TAG, "fb Login attempt failed." + e);
Toast.makeText(LoginActivity.this,R.string.str_please_check_your_internet_connection,Toast.LENGTH_LONG).show();
}
});
private void doTaskWithFbProfile(com.facebook.Profile fbProfile) {
Logger.d("FBLOGIN", "id - " + fbProfile.getId());
Logger.d("FBLOGIN", "name - " + fbProfile.getName());
Logger.d("FBLOGIN", "pic - " + fbProfile.getProfilePictureUri(150, 150));
com.android.circlecare.models.common.Profile profile = new com.android.circlecare.models.common.Profile();
profile.setFBID(fbProfile.getId());
profile.setName(fbProfile.getName());
profile.setProfilePhoto(fbProfile.getProfilePictureUri(150, 150).toString());
Intent intent = new Intent(this, LoginWithFacebookActivity.class);
intent.putExtra(LoginWithFacebookActivity.EXTRA_PROFILE, profile);
startActivity(intent);
finish();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
2)On the other hand I used LoginManager to log in to fb. Where after successful login the login dialog does not get dismissed, instead it stays in front and again after login the dialog doesn't go.
code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
mFacebookCallback = new FacebookCallback<LoginResult>() {
ProfileTracker profileTracker;
#Override
public void onSuccess(LoginResult loginResult) {
Profile fbProfile = Profile.getCurrentProfile();
if (fbProfile == null) {
profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
profileTracker.stopTracking();
getAllFriends();
}
};
profileTracker.startTracking();
} else {
getAllFriends();
}
}
#Override
public void onCancel() {
Logger.d(TAG, "fb Login attempt canceled.");
}
#Override
public void onError(FacebookException error) {
Logger.d(TAG, "Error : " + error.getMessage());
finish();
}
};
callbackManager = CallbackManager.Factory.create();
if (hasAccess)
getAllFriends();
else
loginWorks();
}
private void loginWorks() {
loginManager = LoginManager.getInstance();
loginManager.registerCallback(callbackManager, mFacebookCallback);
ArrayList<String> publishPermission = new ArrayList<>();
publishPermission.add("publish_pages");
publishPermission.add("publish_actions");
loginManager.logInWithPublishPermissions(this, publishPermission);
ArrayList<String> readPermission = new ArrayList<>();
readPermission.add("user_friends");
readPermission.add("public_profile");
readPermission.add("user_about_me");
loginManager.logInWithReadPermissions(this, readPermission);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
I've used almost same codes in both the cases, but I wonder why login dialog isn't get dismissed when working with LoginManager.
Can any one point out the problem or is there any api to dismiss fb login dialog(web view).
In your loginWorks() method, you're literally calling login twice, which is probably actually showing 2 different login screens, and why it looks like the dialog is not dismissing after you finish one of them. Try removing the loginWithPublishPermissions call, and put it in the callback from the read permissions call if you really need it.

Android Facebook Log In with AWS Cognito

I'm having trouble authenticating users with Facebook and creating a single new identity with AWS Cognito. Then if that user deletes the app and re-downloads it I want Cognito to return the same identity.
Facebook login works but credentialsProvider.setLogins(logins); creates 4 NEW unauthenticated identities every time.
final CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(getApplicationContext(), "xxxxxxxx", Regions.US_EAST_1);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("email", "user_friends"));
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(
AccessToken oldAccessToken,
AccessToken currentAccessToken) {
updateWithToken(currentAccessToken);
}
};
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Map<String, String> logins = new HashMap<String, String>();
logins.put("graph.facebook.com", AccessToken.getCurrentAccessToken().getToken());
credentialsProvider.setLogins(logins);
Log.d("LogTag", "my ID is " + logins);
}
#Override
public void onCancel() {
Toast.makeText(getBaseContext(), "Login Cancelled", Toast.LENGTH_SHORT).show();
Log.v("LoginActivity", "cancel");
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(getBaseContext(), "Problem connecting to Facebook", Toast.LENGTH_SHORT).show();
Log.v("LoginActivity", exception.getCause().toString());
}
});
}
private void updateWithToken(AccessToken token) {
if (token != null) {
Intent intent = new Intent(LogIn.this, NavDrawer.class);
LogIn.this.startActivity(intent);
}
;
}
#Override
public void onDestroy() {
super.onDestroy();
accessTokenTracker.stopTracking();
}
#Override
protected void onResumeFragments() {
super.onResumeFragments();
updateWithToken(AccessToken.getCurrentAccessToken());
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
Intent intent = new Intent(LogIn.this, Buffer.class);
LogIn.this.startActivity(intent);
}
}
Any help would be amazing, thanks you!
I can see you are starting a new activity every time your access token from Facebook is updated. That mean you are creating different instances of CognitoCachingCredentialsProvider every time, which is not right.
Ideally you should have CognitoCachingCredentialsProvider as a singleton in your application and use is in all you activities and app logic. You can use the Cognito sample application as an example to model the flow.

Android Facebook - get null profile

First time with Facebook SDK. I can't get users profile. Its always null. What's wrong?
btnFbWidget = (LoginButton) findViewById(R.id.btnFbWidget);
btnFbWidget.setReadPermissions("public_profile");
// Callback registration
btnFbWidget.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
if (profile != null)
Toast.makeText(getApplicationContext(), "LogIN as " + profile.getName(), Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "nulll", Toast.LENGTH_SHORT).show();
}
/*...*/
});
use this method also,.....
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Try with this method.
Initialize facebook SDK firstly.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init facebook sdk and
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
//instantiate callbacks manager
mCallbackManager = CallbackManager.Factory.create();
btnFbWidget=(LoginButton)findViewById(R.id.login_button);
btnFbWidget.registerCallback(mCallbackManager, this);
btnFbWidget.setReadPermissions(Arrays.asList("user_status","email"));
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//manage login result
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
public void onSuccess(LoginResult loginResults) {
Toast.makeText(getApplicationContext(), "Success ",
Toast.LENGTH_LONG).show();
}
public void onCancel() {
}
public void onError(FacebookException e) {
}
And then to get the User Profile use this method.
private static String uid,email;
public void getUserProfile() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.v("LoginActivity", response.toString());
try {
//and fill them here like so.
uid = object.getString("id");
email = object.getString("email")
} catch (JSONException e) {
e.printStackTrace();
}
getGroups();
}
});
// If you want to pass data to other activity.
Bundle parameters = new Bundle();
parameters.putString("userdata", "email");
request.setParameters(parameters);
request.executeAsync();
}
And do call this method getUserProfile() in your on create.
EDIT 1 :
Change in read permissions.
btnFbWidget.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
And accordingly you can get further information like.
String email = object.getString("email");
String birthday = object.getString("birthday");

Facebook CallbackManager is unable to call registeredCallback

I am trying to integrate FB Graph API to enable user to share the content on their FB wall.
On app launch user can login using the facebook button (LoginButton) which only uses read permission (to get the user name and picture).
Next when the user want to post a picture on their wall using my app; I create another instance of LoginButton and an instance to CallbackManager interface, then attach some code to that callbackManager (Code given below).
final LoginButton mLoginButton = new LoginButton(this);
mLoginButton.setPublishPermissions("publish_actions");
mPublishHandler = CallbackManager.Factory.create();
if (mPublishHandler != null) {
mLoginButton.registerCallback(mPublishHandler, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult result) {
// post picture code
}
#Override
public void onError(FacebookException error) {
Toast.makeText(MainScreen.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
#Override
public void onCancel() {
Toast.makeText(MainScreen.this, "fb login was canceled", Toast.LENGTH_LONG).show();
}
});
}
and the callback is invoked in the onActivityResult() method
if (mPublishHandler != null) {
mPublishHandler.onActivityResult(requestCode, resultCode, data);
}
My problem is that when I tried to use the on the fly login button with publish permission; the facebook sdk cannot find the callback I set with even though it is returning success.
08-10 02:27:58.972: D/FacebookSDK.Response(4992): Response (raw)
08-10 02:27:58.972: D/FacebookSDK.Response(4992): Size: 16
08-10 02:27:58.972: D/FacebookSDK.Response(4992): Response:
08-10 02:27:58.972: D/FacebookSDK.Response(4992): {"success":true}
So, I tried to track the issue and find this problem occurring within the FB SDK -
(inside onActivityResult#com.facebook.internal.CallbackManagerImpl)
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
Callback callback = callbacks.get(requestCode);
if (callback != null) {
return callback.onActivityResult(resultCode, data);
}
return runStaticCallback(requestCode, resultCode, data);
}
Here the callback is a hashmap which facebook is probably using for storing callback instances(V) against requestCode(K). But I am getting requestCode as 64207 while checking the callback hashmap I can see that there is indeed an instance with key value 64206. Thus returning callback as null.
Any indication or hint is appreciated, thanks in advance.
You should call and register the callbackManger using the same variable
Also, call it in onActivityResult.
for example:
public class Scene extends Activity {
CallbackManager callbackManager;
ShareDialog shareDialog;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
lms = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
// this part is optional
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) { print("onSuccess"); }
#Override
public void onCancel() { print("onCancel"); }
#Override
public void onError(FacebookException error) { print("onError"); }
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private void share() {
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription(
"The 'Hello Facebook' sample showcases simple Facebook integration")
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
shareDialog.show(linkContent);
}
}
}

Categories

Resources