gpt4 book ai didi

java - 以页面而非用户身份在 Facebook 页面上发帖

转载 作者:行者123 更新时间:2023-12-01 12:44:50 25 4
gpt4 key购买 nike

我正在尝试以管理员身份发布到 Facebook 页面。该帖子始终显示为用户,而不是该帖子由页面保存。

这是代码,我在用户墙上发布了一个示例,并添加了manages_page权限并将me/feed更改为page_id

Java 文件:

public class MainFragment extends Fragment {

private static final String TAG = "MainFragment";

private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(final Session session, final SessionState state,
final Exception exception) {
onSessionStateChange(session, state, exception);
}
};

private Button shareButton;

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");


private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";

private boolean pendingPublishReauthorization = false;
TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
text = (TextView)view.findViewById(R.id.textView1);
LoginButton authButton = (LoginButton) view
.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setPublishPermissions("manage_pages");
shareButton = (Button) view.findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
publishStory();
}
});

if (savedInstanceState != null) {
pendingPublishReauthorization = savedInstanceState.getBoolean(
PENDING_PUBLISH_KEY, false);
}
return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}

@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 onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}

@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);
outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization);
uiHelper.onSaveInstanceState(outState);
}

private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
shareButton.setVisibility(View.VISIBLE);
if (pendingPublishReauthorization
&& state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
pendingPublishReauthorization = false;
publishStory();
}
} else if (state.isClosed()) {
shareButton.setVisibility(View.INVISIBLE);
}
}

private void publishStory() {
final Session session = Session.getActiveSession();
if (session != null) {

// Check for publish permissions
List<String> permissions = session.getPermissions();

if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);


// session.open(session.getAccessToken(), callback);
return;
}

final Bundle postParams = new Bundle();
postParams.putString("message", "Testing app");


final Request.Callback callback = new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(TAG, "JSON error " + e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(getActivity().getApplicationContext(),
error.getErrorMessage(), Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(getActivity().getApplicationContext(),
postId, Toast.LENGTH_LONG).show();
}
}
};

Request request = new Request(session, "667572223265719/feed", postParams,
HttpMethod.POST, callback);

RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}

private boolean isSubsetOf(Collection<String> subset,
Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}

}

最佳答案

显然您没有获取页面访问 token ,而是使用用户访问 token 。请求 manage_pages 权限是正确的做法,但之后您需要调用

/me/accounts

获取各个页面的访问 token 。之后,您必须使用此访问 token 向

{page_id}/feed

关于java - 以页面而非用户身份在 Facebook 页面上发帖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24804068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com