Permission denied when downloading a video on Facebook in Android

advertisements

I'm writing an android app to upload and share a video on the facebook. My code is as below:

 public void doShareVideo(View view) {
        List<String> permissionNeeds = Arrays.asList("publish_actions","publish_pages");
        //this loginManager helps you eliminate adding a LoginButton to your UI
        LoginManager manager = LoginManager.getInstance();

        manager.logInWithPublishPermissions(this, permissionNeeds);

        Uri videoFileUri = Uri.parse("file:///storage/emulated/0/Movies/Untitled.mp4");
        ShareVideo video = new ShareVideo.Builder()
                .setLocalUrl(videoFileUri)
                .build();
        ShareVideoContent content = new ShareVideoContent.Builder()
                .setVideo(video)
                .setContentTitle("Video shared from my android apps")
                .build();

        ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {
                error.printStackTrace();
            }
        });
    }

But I get the error message when running:

10-10 02:57:40.714 19490-19490/? W/System.err: Error preparing share content: Permission denied
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.internal.ShareInternalUtility.invokeOnErrorCallback(ShareInternalUtility.java:529)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.internal.ShareInternalUtility.invokeCallbackWithError(ShareInternalUtility.java:98)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.internal.ShareInternalUtility.invokeCallbackWithException(ShareInternalUtility.java:90)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.ShareApi.shareVideoContent(ShareApi.java:417)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.ShareApi.share(ShareApi.java:186)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.facebook.share.ShareApi.share(ShareApi.java:79)
10-10 02:57:40.714 19490-19490/? W/System.err:     at com.anhnn.facebooklogin.ShareLinkActivity.doShareVideo(ShareLinkActivity.java:189)
10-10 02:57:40.714 19490-19490/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
10-10 02:57:40.714 19490-19490/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
10-10 02:57:40.714 19490-19490/? W/System.err:     at android.view.View$1.onClick(View.java:4015)
10-10 02:57:40.714 19490-19490/? W/System.err:     at android.view.View.performClick(View.java:4780)
10-10 02:57:40.714 19490-19490/? W/System.err:     at android.view.View$PerformClick.run(View.java:19866)
10-10 02:57:40.715 19490-19490/? W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
10-10 02:57:40.715 19490-19490/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-10 02:57:40.715 19490-19490/? W/System.err:     at android.os.Looper.loop(Looper.java:135)
10-10 02:57:40.715 19490-19490/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
10-10 02:57:40.715 19490-19490/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
10-10 02:57:40.715 19490-19490/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
10-10 02:57:40.715 19490-19490/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-10 02:57:40.715 19490-19490/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
10-10 02:57:40.757 19490-19490/? W/art: Attempt to remove local handle scope entry from IRT, ignoring
10-10 02:57:40.758 19490-19490/? W/AwContents: onDetachedFromWindow called when already detached. Ignoring

I use Facebook SDK: com.facebook.android:facebook-android-sdk:4.6.0 Please help me and thank you very much.


To @Fyodor Volchyok, I already changed my code as your suggestion, but nothing happens to me. No log information or no error is showed in the LogCat after log-in the Facebook and approving the permissions successfully. My new code is as below:

public class ShareVideoActivity extends AppCompatActivity {

    private static final String LOG_TAG = "ShareVideo";
    private CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(this.getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_video);

        callbackManager = CallbackManager.Factory.create();
        LoginManager loginManager = LoginManager.getInstance();
        loginManager.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // it would be nice here to check loginResult.getRecentlyGrantedPermissions() for requested permissions
                        Log.d(LOG_TAG, "Success " + loginResult.getRecentlyGrantedPermissions().toString());
                        Uri videoFileUri = Uri.parse("file:///storage/emulated/0/Movies/Untitled.mp4");
                        ShareVideo video = new ShareVideo.Builder()
                                .setLocalUrl(videoFileUri)
                                .build();
                        ShareVideoContent content = new ShareVideoContent.Builder()
                                .setVideo(video)
                                .setContentTitle("Video shared from my android apps")
                                .build();

                        ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
                            @Override
                            public void onSuccess(Sharer.Result result) {

                            }

                            @Override
                            public void onCancel() {

                            }

                            @Override
                            public void onError(FacebookException error) {
                                error.printStackTrace();
                            }
                        });
                    }

                    @Override
                    public void onCancel() {
                        // App code
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                    }
                });
        List<String> permissionNeeds = Arrays.asList("publish_actions", "publish_pages");
        loginManager.logInWithPublishPermissions(this, permissionNeeds);
    }
}

Do you have any new suggestion for me? Thanks in advance. (I can share Image and Link but encountering the problem with Video)


It seems like you're trying to upload video when permissions hasn't been granted yet. Try the following:

  1. Set callbacks for LoginManager.
  2. Log in with LoginManager.
  3. (MOST IMPORTANT!) Wait for user to log in. You will be notified via callbacks.
  4. Check whether permissions granted.
  5. If so, upload.

Approximate result could look like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager,
    new FacebookCallback < LoginResult > () {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // it would be nice here to check loginResult.getRecentlyGrantedPermissions() for requested permissions

            Uri videoFileUri = Uri.parse("file:///storage/emulated/0/Movies/Untitled.mp4");
            ShareVideo video = new ShareVideo.Builder()
                .setLocalUrl(videoFileUri)
                .build();
            ShareVideoContent content = new ShareVideoContent.Builder()
                .setVideo(video)
                .setContentTitle("Video shared from my android apps")
                .build();

            ShareApi.share(content, new FacebookCallback < Sharer.Result > () {
                @Override
                public void onSuccess(Sharer.Result result) {

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException error) {
                    error.printStackTrace();
                }
            });
        }

        @Override
        public void onCancel() {
            // App code
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
        }
    });
    LoginManager.getInstance().logInWithPublishPermissions(this, permissionNeeds);
}

And yes, this code could and should be improved, but the whole idea is here.