我正在尝试通过以下代码在用户提要中共享一个链接,并且只有我使用开发人员帐户登录才能使用。如果我尝试通过任何其他用户帐户发布它,它只会给出一个帖子 ID,但没有可见的帖子。
MainFragment 仅包含布局充气器,该布局只有一个 TextView 和 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);
}
}