Reputation:
On my main activity, I have the following code:
FacebookSdk.sdkInitialize(getApplicationContext());
I also added a button to create a post on my Facebook timeline:
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
}
});
}
When I run my app on my smartphone, and click the button, I always get the following message (onError callback):
Insufficient permissions for sharing content via Api.
I have configured com.facebook.app.FacebookContentProvider, com.facebook.sdk.ApplicationId, com.facebook.FacebookActivity in Manifest file. And also have registered my app on facebook.
What is missing, please? Should I add a facebook Login button on my app?
Upvotes: 2
Views: 1566
Reputation:
I have discovered that this error occurs when AccessToken.getCurrentAccessToken() returns null. So I must authenticate to Facebook explicitly before using the ShareAPI.
I saw it in Facebook's source code available at 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"));
}
Upvotes: 1