我想将帖子发布到用户的墙上。我知道Facebook只允许测试人员和开发人员在墙上发帖。我已将用户添加到测试人员列表中。当我尝试获得发布权限时,它表示用户已经授予权限(如屏幕截图所示)并返回。我无法获得许可或在墙上张贴。而且,回调的任何方法都不会被调用。
CODE
我已经关注了Facebook示例RPSSample中的代码。
//Publish to wall
public void publishResult() {
registerPublishPermissionCallback();
if (canPublish()) { //see definition below
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(urlToPost))
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
callback.didShareOnFacebookSuccessfully();
}
@Override
public void onCancel() {
// This should not happen
}
@Override
public void onError(FacebookException error) {
showToast(error.getMessage());
}
});
}
}
//check if user has permission or not
private boolean canPublish() {
final AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken != null) {
if (accessToken.getPermissions().contains(AppConstants.ADDITIONAL_PERMISSIONS)) {
// if we already have publish permissions, then go ahead and publish
return true;
} else {
// otherwise we ask the user if they'd like to publish to facebook
new AlertDialog.Builder(activity)
.setTitle(R.string.share_with_friends_title)
.setMessage(urlToPost)
.setPositiveButton(R.string.share_with_friends_yes, canPublishClickListener)
.setNegativeButton(R.string.share_with_friends_no, dontPublishClickListener)
.show();
return false;
}
}
return false;
}
//If user allows, ask Facebook to grant publish_action permission
private DialogInterface.OnClickListener canPublishClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (AccessToken.getCurrentAccessToken() != null) {
// if they choose to publish, then we request for publish permissions
LoginManager.getInstance()
.setDefaultAudience(DefaultAudience.FRIENDS)
.logInWithPublishPermissions(activity,
Arrays.asList(AppConstants.ADDITIONAL_PERMISSIONS));
}
}
};
//Callback - Any of the method doesn't call.
private void registerPublishPermissionCallback() {
LoginManager.getInstance().registerCallback(
callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken.getPermissions().contains(AppConstants.ADDITIONAL_PERMISSIONS)) {
publishResult();
} else {
handleError("Not enough permissions to publish");
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
handleError(exception.getMessage());
}
private void handleError(String errorMessage) {
// this means the user did not grant us write permissions, so
// we don't do implicit publishes
showToast(errorMessage);
}
}
);
}
我的应用程序在控制台上运行。请指导Facebook的哪些要求获得测试用户的发布权限?感谢。
答案 0 :(得分:0)
使用以下代码检查是否授予许可。
String url = "/" + "(userFbID)" + "/permissions";
new GraphRequest(AccessToken.getCurrentAccessToken(), url, null,
HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONObject json = new JSONObject(
response.getRawResponse());
JSONArray data = json.getJSONArray("data");
boolean isPermitted = false;
for (int i = 0; i < data.length(); i++) {
if (data.getJSONObject(i)
.getString("permission")
.equals("publish_actions")) {
isPermitted = true;
String status = data.getJSONObject(i)
.getString("status");
if (status.equals("granted")) {
publishResult()
} else {
LoginFacebook();
}
break;
}
}
if (!isPermitted) {
LoginFacebook();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).executeAsync();
替换用户的fb id代替(userFbID)。
如果未授予权限,则使用此功能向用户询问该权限。
private void LoginFacebook() {
loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(this,
Arrays.asList("publish_actions"));
loginManager.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
publishResult()
}
@Override
public void onError(FacebookException error) {
}
@Override
public void onCancel() {
}
});
}
答案 1 :(得分:0)
终于能够解决问题了。代码中没有问题。这是Facebook应用程序的一些设置/权限问题。这是我遵循的步骤:
希望这有助于某人!