在我的主要活动中,我有以下代码:
FacebookSdk.sdkInitialize(getApplicationContext());
我还添加了一个按钮,用于在我的Facebook时间线上创建帖子:
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Result arg0) {
// TODO Auto-generated method stub
}
@Override
public void onError(FacebookException arg0) {
// TODO Auto-generated method stub
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
}
当我在智能手机上运行我的应用程序并单击按钮时,我总是收到以下消息(onError回调):
通过Api共享内容的权限不足。
我在Manifest文件中配置了com.facebook.app.FacebookContentProvider,com.facebook.sdk.ApplicationId,com.facebook.FacebookActivity。并且还在facebook上注册了我的应用程序。
有什么遗漏,拜托?我应该在我的应用程序上添加Facebook登录按钮吗?
答案 0 :(得分:1)
我发现当AccessToken.getCurrentAccessToken()返回null时会发生此错误。因此,在使用ShareAPI之前,我必须明确地对Facebook进行身份验证。
我在Facebook的http://android-education-project.googlecode.com/svn/trunk/facebook_4_0_0/src/com/facebook/share/ShareApi.java提供的源代码中看到了它:
/**
* Share the content.
*
* @param callback the callback to call once the share is complete.
*/
public void share(FacebookCallback<Sharer.Result> callback) {
if (!this.canShare()) {
ShareInternalUtility.invokeCallbackWithError(
callback, "Insufficient permissions for sharing content via Api.");
return;
}
final ShareContent shareContent = this.getShareContent();
// Validate the share content
try {
ShareContentValidation.validateForApiShare(shareContent);
} catch (FacebookException ex) {
ShareInternalUtility.invokeCallbackWithException(callback, ex);
return;
}
if (shareContent instanceof ShareLinkContent) {
this.shareLinkContent((ShareLinkContent) shareContent, callback);
} else if (shareContent instanceof SharePhotoContent) {
this.sharePhotoContent((SharePhotoContent) shareContent, callback);
} else if (shareContent instanceof ShareVideoContent) {
this.shareVideoContent((ShareVideoContent) shareContent, callback);
} else if (shareContent instanceof ShareOpenGraphContent) {
this.shareOpenGraphContent((ShareOpenGraphContent) shareContent, callback);
}
}
/**
* Returns true if the current access token has the publish_actions permission.
*
* @return true if the current access token has the publish_actions permission, false otherwise.
*/
public boolean canShare() {
if (this.getShareContent() == null) {
return false;
}
final AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken == null) {
return false;
}
final Set<String> permissions = accessToken.getPermissions();
if (permissions == null) {
return false;
}
return (permissions.contains("publish_actions"));
}