当我使用HelloFacebookSample时,代码运行正常,我可以在用户墙上发布。
但是当我尝试发布视频和embeded it on the wall时,我使用了publish to feed tutorial
当我转换代码时,我将postStatusUpdate()
替换为publishStory()
public void postStatusUpdate() {
if (user != null && hasPublishPermission()) {
final String message ;
message = ( user.getFirstName()+" "+ getString( R.string.status_update_link)+ " " +video_id + " " + getString(R.string.google_play_link));
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, new Request.Callback() {
@Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
替换为:
private void publishStory() {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("message", getString( R.string.google_play_link));
postParams.putString("link", "http://www.youtube.com/watch?v="+ video_id);
postParams.putString("source", "http://www.youtube.com/v/" + video_id);
postParams.putString("picture","http://img.youtube.com/vi/" +video_id + "/0.jpg");
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(MyApp.intense,
error.getErrorMessage(),
Toast.LENGTH_LONG).show();
} else {
/**
Toast.makeText(MyApp.intense,
// postId
"Sent"
,
Toast.LENGTH_LONG).show();
*/
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
因此,当我尝试发布时,该应用程序是cresh并且我收到“会话:尝试为具有待处理请求的会话请求新权限”
我没有找到第一次许可......
在以这种形式发布的其他Q中,开发人员接收到与我收到的相同的错误MSG,但看起来这个错误出现在session is not OPENED和用户调用它时,我相信这不是问题
我从这个FragmentActivity附加了更多代码,因为我认为这是必要的。
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (pendingAction != PendingAction.NONE &&
(exception instanceof FacebookOperationCanceledException ||
exception instanceof FacebookAuthorizationException)) {
new AlertDialog.Builder(HelloFacebookSampleActivity.this)
.setTitle(R.string.cancelled)
.setMessage(R.string.permission_not_granted)
.setPositiveButton(R.string.ok, null)
.show();
postStatusUpdateButton.setEnabled(false);
pendingAction = PendingAction.NONE;
} else if (state == SessionState.OPENED_TOKEN_UPDATED) {
handlePendingAction();
}
updateUI();
}
private enum PendingAction {
NONE,
POST_STATUS_UPDATE
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
答案 0 :(得分:1)
您只需添加pendingPublishReauthorization = true;
//so
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);
return;
}
它为我工作。
希望这个帮助
答案 1 :(得分:0)
您对session.requestNewPublishPermissions(newPermissionsRequest);
的通话将生成一个Facebook对话框,要求用户提供权限。您需要@Override onActivityResult()
捕获并应用Facebook对话框中返回的内容。在我的应用程序中,我首先要求用户验证我的应用程序,然后请求访问其组的权限...
public class FacebookManage extends Activity {
// Facebook
private Session fbSession = null;
private int authCount = 0;
// There is a UI button that calls addAccount();
public void addAccount(View v) {
// Open a new Session
OpenRequest or = new Session.OpenRequest(this);
or.setCallback(null);
// Login
Session.Builder sb = new Session.Builder(this);
Session session = sb.build();
Session.setActiveSession(session);
// This will bring up a Facebook dialog and return a call to onActivityResult
session.openForRead(or);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE) {
if (authCount == 0) {
// This is the first authentication where we got basic access to the user account
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
fbSession = Session.getActiveSession();
if (fbSession != null && !fbSession.isClosed()) {
// Ask for Group permission
authCount++;
List<String> fbPermissions = new ArrayList<String>();
fbPermissions.add("user_groups");
NewPermissionsRequest snpr = new NewPermissionsRequest(this, fbPermissions);
fbSession.requestNewReadPermissions(snpr);
}
} else if (authCount == 1) {
// Returning from asking for access to "Groups"
if (fbSession != null && !fbSession.isClosed()) {
// This will add the group permission to the current session
fbSession.onActivityResult(this, requestCode, resultCode, data);
}
}
}
}
我实际上要求三轮请求权限(初始,组,页面和发布),然后使用authCount
来跟踪我的位置。用户看到4个Facebook对话框,但他们有好处,但每个单独的“权限”请求。你明白了......
我完成后只需致电fbSession.close();
。然后,访问令牌由Facebook SDK缓存和管理。通过使用一个Activity来执行身份验证并让SDK跟踪它,在其他活动(页面)中,我只需要调用fbSession = Session.openActiveSessionFromCache(this);
,我就可以了。
(注意 - 如果用户进入他们的Facebook设置并且特别是核实了您最初要求的一些权限,那么在您尝试发布之前,您将不会知道它。啊,错误捕获......)