我知道这个问题之前已被多次询问过。但这些答案都不适合我。这似乎是许可的一些问题。它似乎没有存储发布权限,并且每次用户尝试发布内容时都会不断询问权限。这是我的代码。
public class MainActivity extends Activity {
private Session.StatusCallback statusCallback = new SessionStatusCallback();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn_post);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
postToFaceBook();
}
});
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
//get the active Facebook session
Session session = Session.getActiveSession();
//If no active sessions found
if (session == null) {
if (savedInstanceState != null) {
//Restore the saved session from bundle if any
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if (session == null) {
//If the session has not been stored in bundle, create a new session
session = new Session(this);
}
//set session as current active session
Session.setActiveSession(session);
//If the session has not been opened yet, log the user into facebook
//It does not involve interaction of user with facebook
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
}
@Override
protected void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
@Override
protected void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, intent);
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (exception != null) {
exception.printStackTrace();
}
}
}
private void postToFaceBook() {
Log.i("fb", "inside share via facebook");
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
//Interact with the user to get user name and password for facebook
session.openForRead(new Session.OpenRequest(MainActivity.this).setCallback(statusCallback));
} else {
Session.openActiveSession(MainActivity.this, true, statusCallback);
}
Bundle postParams = new Bundle();
postParams.putString("message", "testing");
postParams.putString("link", "https://www.youtube.com/watch?v=GcNNx2zdXN4");
//get the current active facebook session
session = Session.getActiveSession();
//If the session is open
if(session.isOpened()) {
Log.i("fb", "inside isopened");
//Get the list of permissions associated with the session
List<String> permissions = session.getPermissions();
//if the session does not have video_upload permission
if(!permissions.contains("publish_actions") || !permissions.contains("publish_stream")) {
Log.i("fb", "doesn't contain permission");
//Get the permission from user to upload the video to facebook
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(MainActivity.this, Arrays.asList("publish_actions" , "publish_stream"));
session.requestNewPublishPermissions(newPermissionsRequest);
}
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
Log.i("fb", "On completed");
FacebookRequestError error = response.getError();
if (error != null) {
//AppUtils.showToastMessage(th, error.getErrorMessage());
Toast.makeText(MainActivity.this, error.getErrorMessage(), Toast.LENGTH_SHORT).show();
} else {
//AppUtils.showToastMessage(mActivity, mResources.getString(R.string.posted_successfully));
//AppUtils.setSharedOrigami(mActivity, mPortraitKey);
Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
}
//mFacebookDialog.dismiss();
}
};
Request request = new Request(session,
"me/feed", postParams, HttpMethod.POST, callback);
Log.i("fb", "posting");
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
}
清单:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
在使用publish_action
权限之前,是否需要提交应用以供审核?任何帮助,将不胜感激。谢谢!
答案 0 :(得分:1)
你所拥有的是一个常见问题。问题是您在调用requestNewPublishPermissions后立即向我发送请求/提要。
但是,requestNewPublishPermissions是一个异步请求,因此当您向我/ Feed发帖时,您实际上还没有发布权限。您需要保存您即将发布的事实,请求发布权限,然后当该请求返回时(在onActivityResult中或在回调中),请将帖子发送到&#34; me / feed&#34 ;然后
查看SDK附带的Hello Facebook示例应用程序,它会向您展示如何请求发布权限,并推迟发布,直到您拥有所有权限。