public class NextActivity extends AppCompatActivity {
CallbackManager callbackManager;
LoginManager manager;
ImageView image;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Button shareButton = (Button)findViewById(R.id.sharebtn);
image = (ImageView) findViewById(R.id.image);
FacebookSdk.sdkInitialize(getApplicationContext());
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkpermission();
}});
}
public void checkpermission(){
final Bundle permBundle = new Bundle();
permBundle.putCharSequence("permission", "publish_actions");
GraphRequest request = new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/permissions", permBundle, HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
System.out.println("sammy_permission_graph_request "+graphResponse.getJSONObject());
try {
JSONArray permList = (JSONArray) graphResponse.getJSONObject().get("data");
if(permList.length() == 0){
// no data for perms, hence asking permission
getPublishPermission();
}else{
JSONObject permData = (JSONObject) permList.get(0);
String permVal = (String) permData.get("status");
if(permVal.equals("granted")){
sharePhotoToFacebook();
}else{
getPublishPermission();
}
}
} catch (JSONException e) {
System.out.println("sammy_exception while parsing fb check perm data "+e.toString());
}
}
}
);
request.executeAsync();
}
public void getPublishPermission(){
callbackManager = CallbackManager.Factory.create();
manager = LoginManager.getInstance();
manager.logInWithPublishPermissions(
NextActivity.this,
Arrays.asList("publish_actions"));
manager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("sammy_got_publish_permission");
sharePhotoToFacebook();
}
@Override
public void onCancel() {
System.out.println("sammy_cancelled while asking publish permission");
}
@Override
public void onError(FacebookException error) {
System.out.println("sammy_error occurred while asking publish permission!");
}
});
}
private void sharePhotoToFacebook(){
System.out.println("sammy_sharing_started");
try{
Bitmap bm = ((BitmapDrawable)image.getDrawable()).getBitmap();
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(bm)
.setCaption("BMW Vision")
.setUserGenerated(true)
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result)
{
System.out.println("sammy_publish_result "+result);
Toast.makeText(NextActivity.this, "Success in Posting", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel()
{
System.out.println("sammy_shareapi_canceled");
}
@Override
public void onError(FacebookException e)
{
System.out.println("sammy_shareapi_exception "+e);
}
});
}catch (Exception e){
System.out.println("sammy_exception "+e.toString());
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
我正在尝试在后台与Facebook分享图片和文字。首先,我正在检查是否已授予发布权限。如果不是,我正在使用logInWithPublishPermissions
获取发布权限。但由于某种原因,registerCallback的响应始终在onCancel()
。我需要添加到logInWithPublishPermissions?
答案 0 :(得分:0)
您必须在Facebook开发者网站https://developers.facebook.com/
上授予 publish_actions 权限请按照link设置您的应用以访问publish_actions权限并提交您的应用以供审核。