在这个游戏中,用户可以通过FaceBook API登录,然后通过得分api在Facebook上分享最佳得分。
但是当我通过我的Facebook帐户登录游戏时,每件事情都有效,当我通过其他帐户登录时,我收到了这个错误:
(#200)需要扩展权限:publish_actions
这是我的代码:
public void postScore() {
final int score = getScore();
Log.i(TAG, "" + score);
if (score > 0) {
// Only post the score if they smashed at least one friend!
// Post the score to FB (for score stories and distribution)
Bundle fbParams = new Bundle();
fbParams.putString("score", "" + score);
Request postScoreRequest = new Request(Session.getActiveSession(),
"me/scores", fbParams, HttpMethod.POST,
new Request.Callback() {
@Override
public void onCompleted(Response response) {
Log.i(TAG, "onCompleted");
FacebookRequestError error = response.getError();
if (error != null) {
Log.e(TAG, "Posting Score to Facebook failed: "
+ error.getErrorMessage());
} else {
Log.i(TAG,
"Score posted successfully to Facebook");
}
}
});
Request.executeBatchAsync(postScoreRequest);
}
}
请帮帮我。
答案 0 :(得分:1)
我通过这个步骤解决问题:
<强>步骤1 强>:
postButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Session session = Session.getActiveSession();
if (session == null || !session.isOpened()) {
Log.i(TAG, "Session is null!");
return;
}
List<String> permissions = session.getPermissions();
if (!permissions.contains("publish_actions")) {
// the user didn't grant this permission, so we need to
// prompt them.
askForPublishActionsForScores();
return;
} else {
ReadAndWriteFBscore.postScore();
}
}
});
<强>第二步强>:
private void askForPublishActionsForScores() {
new AlertDialog.Builder(MainActivity.this)
.setPositiveButton("بله",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// User hit OK. Request Facebook friends
// permission.
requestPublishPermissions();
}
})
.setNegativeButton("خیر",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User hit cancel.
}
}).setTitle(R.string.publish_scores_dialog_title)
.setMessage(R.string.publish_scores_dialog_message).show();
}
<强>步骤3 强>:
void requestPublishPermissions() {
Log.d(TAG, "Requesting publish permissions.");
final Session session = Session.getActiveSession();
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(AUTH_PUBLISH_ACTIONS_SCORES_ACTIVITY_CODE);
session.requestNewPublishPermissions(newPermissionsRequest);
}
}
<强>步骤4 强>:
private static final int AUTH_PUBLISH_ACTIONS_SCORES_ACTIVITY_CODE = 103;