Reputation: 436
I'm trying to share a link in the user feed through the below code and it will work only I log in using the developer account. If I try to post it through any other user account it will only give a post ID but there is no visible post.
MainFragment contains only the layout inflater which this layout has only a TextView and facebook LoginButton
package com.chamika.fbtest;
import java.util.Arrays;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.FacebookRequestError;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionDefaultAudience;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
public class MainActivity extends FragmentActivity {
private UiLifecycleHelper uiHelper;
private Context context;
private static final String TAG = "MainFragment";
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions","publish_stream");
private static final int REAUTH_ACTIVITY_CODE = 100;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private MainFragment mainFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
// Session.getActiveSession().onActivityResult(this, requestCode,
// resultCode, data);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
final TextView text = (TextView) findViewById(R.id.welcome);
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
text.setText("Logged in...");
text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
publish();
}
});
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
text.setText("Logged out...");
text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
text.setText("Please log");
}
});
}
}
private void publish() {
Session session = Session.getActiveSession();
if (session == null || !session.isOpened()) {
return;
}
List<String> permissions = session.getPermissions();
if (!permissions.containsAll(PERMISSIONS)) {
requestPublishPermissions(session);
return;
}
// Show a progress dialog because sometimes the requests can take a
// while.
final ProgressDialog progressDialog = ProgressDialog.show(this, "requesting permission",
"requesting additional permission", true);
Bundle postParams = new Bundle();
postParams.putString("name", "Test post");
postParams.putString("caption", "This is a test post");
postParams.putString("description",
"Description of the test post");
postParams.putString("link", "https://developers.facebook.com/android");
postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
Request.Callback callback = new Request.Callback() {
public void onCompleted(Response response) {
progressDialog.dismiss();
JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
Log.d(TAG, "postId=" + postId);
} catch (JSONException e) {
Log.i(TAG, "JSON error " + e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(context.getApplicationContext(), error.getErrorMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context.getApplicationContext(), postId, Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
private void requestPublishPermissions(Session session) {
if (session != null) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS)
// demonstrate how to set an audience for the publish permissions,
// if none are set, this defaults to FRIENDS
.setDefaultAudience(SessionDefaultAudience.FRIENDS).setRequestCode(REAUTH_ACTIVITY_CODE);
session.requestNewPublishPermissions(newPermissionsRequest);
}
}
@Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
// Session session = Session.getActiveSession();
// if (session != null && (session.isOpened() || session.isClosed())) {
// onSessionStateChange(session, session.getState(), null);
// }
uiHelper.onResume();
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
Upvotes: 1
Views: 2825
Reputation: 548
Bundle params = new Bundle();
params.putString("name", "Your App Name or any desired name");
params.putString("description", "Message to share");
params.putString("picture", "Image Url");
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(context,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
FacebookHelper.this.mShareDelegate
.shareStatus();
} else {
// User clicked the Cancel button
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
} else {
// Generic, ex: network error
}
}
})
.build();
feedDialog.show();
Upvotes: 0
Reputation: 12664
By default app is in Sandbox Mode(only visible to Admins,Developers and Testers)
Disable sandbox mode.(To visible all users)
Upvotes: 2
Reputation: 2454
You have to add the Explicit Sharing parameter (fb:explicitly_shared
).
As said on this page: "Explicitly shared actions are eligible to appear as stand-alone stories in the news feed".
This thing applies to the Open Graph specification, but I think that is valid also in your case.
Upvotes: 0
Reputation: 1417
I assume you might have forgotten to add the tester privileges to the facebook accounts that you tried other than the developer account. As you are developing your app it will be in Sandbox mode in which only developer account and tester accounts will be having access.Go to your apps in developer.facebook.com and add tester accounts.
Upvotes: 0