Post 视频从 Android 发送到 Facebook,深度 link 返回到应用程序

Post video to Facebook from Android with deep link back to app

问题: 我正在尝试将内容从我的 Android 应用程序共享到 Facebook,该应用程序托管在其他地方,并让它深层链接回我的应用程序。

详情 我正在开发和 Android 应用程序,我面临的问题是我有一个视频,我想通过返回到应用程序的深层链接分享到用户的新闻源。该视频托管在 Cloudinary 上,但这无关紧要,尝试 post 将 Youtube 视频发送到用户的新闻源,但当他们点击下面的描述时将其深层链接回应用程序将是同样的问题。

我已经非常接近使用 Facebook 的 ShareLinkContentShareDialog。代码如下所示:

ShareLinkContent content = new ShareLinkContent.Builder()
                        .setContentUrl(Uri.parse("https://kt4rh.app.goo.gl/naxy")) /* Deeplink back into my app created using Firebase Dynamic Link */
                        .setImageUrl(Uri.parse({path to an image stored on clodinary}))
                        .build();
                ShareDialog shareDialog = new ShareDialog(getActivity());
                shareDialog.show(content);

这显示了一个 图像,当用户点击它时,它带有返回到我的应用程序的深层链接。但是,我想要做的是显示 video(或 gif,都可以)而不是图像。

我已经尝试将视频下载到设备并使用 ShareVideoContent,希望我可以设置 localUrlcontentUrl(类似于我设置缩略图的方式和上面 ShareLinkContent 中的 contentUrl)像这样:

Uri videoUri = Uri.fromFile(new File(localPathToVideo));
                ShareVideo shareVideo = new ShareVideo.Builder()
                        .setLocalUrl(videoUri)
                        .build();
                ShareVideoContent videoContent  = new ShareVideoContent.Builder()
                        .setVideo(shareVideo)
                        .setContentUrl(Uri.parse("https://kt4rh.app.goo.gl/naxy")) /* Deeplink back to app */
                        .build();
                ShareDialog shareDialog = new ShareDialog(getActivity());
                callbackManager = CallbackManager.Factory.create();
                shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
                    @Override
                    public void onSuccess(Sharer.Result result) {}

                    @Override
                    public void onCancel() {}

                    @Override
                    public void onError(FacebookException error) {
                        Log.e(LOG_TAG, error.getMessage());
                    }
                });
                if (shareDialog.canShow(videoContent)) {
                    shareDialog.show(videoContent);
                }

但我只是收到一个错误消息 "Only one of link, photos, and video should be specified.",这意味着我无法设置本地 url 和内容 url。

所以现在我卡住了。似乎这应该是一个相对常见的问题(共享在其他地方托管的内容并将其深层链接回您的应用程序),但我不知道该怎么做。

我认为 Android 不能同时设置 contentUrl 和视频。我遇到了类似的问题,但在删除 .setContentUrl(...).

后它起作用了

您可以做的是将 link 复制到剪贴板并说服用户将 link 粘贴到他们的标题中(即:在打开前显示 pop-up / 警告ShareDialog).

可能还有其他解决方法。如果是这样,我也很想知道。

希望这对您有所帮助。

最终通过为每个要共享的项目(在本例中为旅行行程)创建一个 html 页面并将它们托管在 Firebase 的云存储上来解决此问题。代码如下所示

/**
 * In order to share gifs to facebook and have a animating gif show up, we need to construct an arbitrary HTML page that wraps the
 * gif image that we want to share. When the user clicks on the wrapped html page, it will forward to the real page we want it to show.
 *
 * Facebook uses custom meta properties to determine what image to show in the preview, which is why we need to wrap the actual gif with another image this way
 */
private static final String GIF_IMAGE = "<gif_image_here>";
private static final String REDIRECT_LINK = "<redirect_link_here>";

private static final String SHARE_HTML_PAGE = "<!DOCTYPE html><html>" +
        "<head>" +
        "<meta property=\"og:type\" content=\"video.other\" />" +
        "<meta property=\"og:url\" content=\"" + GIF_IMAGE + "\" />" +
        "<meta property=\"og:image\" content=\"" + GIF_IMAGE + "\" />" +
        "<meta http-equiv=\"refresh\" content=\"0; url=" + REDIRECT_LINK + "\" />" +
        "</head></html>";

public static void shareGifToFacebook(final Activity activity, final String gifUrl, final Trip trip, final ShareStatusListener shareStatusListener) {
    String htmlPageToShare = SHARE_HTML_PAGE.replaceAll(GIF_IMAGE, gifUrl).replaceAll(REDIRECT_LINK, DeeplinkUtils.getTripDeeplink(trip.getUid()));

    // Upload the html page somewhere accessible
    FirebaseStorageUploader.uploadShareHtml(TravellerManager.getInstance().getCurrentlyLoggedInUserUid(), htmlPageToShare, new FirebaseStorageUploader.UploadShareHtmlListener() {
        @Override
        public void onSuccess(String publicUrl) {
            Log.d("ShareUtils", "shareGifToFacebook() publicUrl to be shortened:  " + publicUrl);

            URLShortener.shortUrl(publicUrl, new URLShortener.LoadingCallback() {
                @Override
                public void startedLoading() {

                }

                @Override
                public void finishedLoading(@Nullable String shortUrl) {
                    Log.d("ShareUtils", "shareGifToFacebook() finishedLoading() shortenedUrl " + shortUrl);

                    ShareLinkContent content = new ShareLinkContent.Builder()
                            .setContentUrl(Uri.parse(shortUrl))
                            .setContentDescription(activity.getString(R.string.view_trip_on_wd))
                            .setContentTitle(trip.getName())
                            .build();
                    ShareDialog shareDialog = new ShareDialog(activity);
                    shareDialog.show(content);

                    if (shareStatusListener != null) {
                        shareStatusListener.onShared();
                    }
                }
            });
        }

        @Override
        public void onFailed() {
            Toaster.toastLong(activity.getString(R.string.general_error));
        }
    });
}