是否可以添加一个链接来下载只能通过在 Facebook 上共享来下载的文件?

IT技术 javascript facebook facebook-graph-api download sharing
2021-03-11 20:00:18

这种情况可能吗?

客户访问我的网站,想要下载他们感兴趣的 PDF 技术文档,他们单击“下载”按钮,然后出现一个 Facebook 共享窗口,让他们登录以将其共享到 Facebook。一旦他们单击“共享”并将其张贴在他们的墙上,那么下载就开始了吗?

非常感谢。

伊恩

2个回答

更新

根据 Facebook 的新政策,这种行为是不允许的。需要您自担风险使用它。我对使用它不承担任何责任。

是的,使用JavaScript SDK,它提供了一个response(不再提供)我们将创建一个if语句来查看响应是否有一个post_idif yes 显示下载链接,否则做其他事情(提醒用户,也许?)

演示(API 2.0)(不工作;需要修改)

演示 (API 2.7) 工作 Rev#63

HTML

<div class="container">
    
    <div>
       <p>This file is locked, to unlock and download it, share it</p>
       <p class="hidden">Thanks for sharing, the file is unlocked and ready for download</p>
       <p class="hidden">In order to download this file, you need to share it</p>
    </div>

    <a class="fsl fsl-facebook" href="#" id="facebook-share">
       <span class="fa-facebook fa-icons fa-lg"></span>
       <span class="sc-label">Share on Facebook</span>
    </a>

    <a class="fsl content-download" href="#" id="download-file">
       <span class="fa-download fa-icons fa-lg"></span>
       <span class="sc-label">Download File</span>
    </a>
    
</div>

JavaScript (jQuery)

$('#ShareToDownload').on('click', function(e) {
            e.preventDefault();
            FB.ui({
                  display: 'popup',
                  method:  'share',
                  href:    location.href,
                  },
                  /** our callback **/
                  function(response) {
                          if (response && response.post_id) {
                          /** the user shared the content on their Facebook, go ahead and continue to download **/
                          $('#ShareToDownload').fadeOut(function(){ $('#downloadSection').fadeIn() });    
                          } else {
                          /** the cancelled the share process, do something, for example **/
                          alert('Please share this page to download this file'):
                  }
            });     
}); 

更新

随着发布API 2.0版本饲料对话已被废弃,并用新的现代更换分享对话框所以上面的代码中使用了新的共享对话框

@Wilf,我刚收到 jsFiddle 的回复,他们解锁了小提琴。而且,小提琴得到了更新。请检查小提琴,注意您需要修改 iframe 源以允许弹出窗口工作。这是如何做到这一点stackoverflow.com/a/32721395/2151050
2021-04-18 20:00:18
@Wilf,我相信这些方法在 API2.5 中仍然有效。尽管如此,我会解决 jsFiddle 的问题并检查它是否有效。谢谢你的提醒。
2021-04-22 20:00:18
不好意思,打扰了。这个脚本可用于 API2.5 吗?如果可以,请在此处提供完整的代码。你的演示被禁止了。
2021-04-25 20:00:18
非常感谢亚当!这太棒了!
2021-04-28 20:00:18
@AdamAzad,在我分享完内容之前,您的脚本效果很好。下载按钮不显示。请帮忙!
2021-05-03 20:00:18

谢谢,完美运行!我不知道如何从 JSON 文件中获取下载链接,所以我的做法略有不同,也许不是那么安全。

将此添加到检查响应的部分

$.post('optimus.php', { 'fieldname' : 'download', 'value' : 'yes'});

创建了一个设置会话的新页面(optimus.php)

<?php
    session_start();
    $_SESSION[$_POST['fieldname']] = $_POST['value'];
?>

Download.php 包含以下代码

<?php
session_start();
if($_SESSION['download'] ==  "yes"){
session_destroy();
$file = 'file.zip';

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
} else {
    echo "You didn't share, or you already downloaded the file";
}
?>

因此,当用户共享某些内容时,$_SESSION['download'] 设置为 yes。Download.php 检查它是否是,并且当它是时自动开始下载。此外,会话被销毁,因此他们只能下载一次。