简体   繁体   English

通过javascript将照片分享到Facebook

[英]Share a photo to facebook via javascript

I'm using a very simple javascript to share an URL to facebook wall: 我正在使用一个非常简单的JavaScript将URL共享到Facebook墙:

window.open('https://www.facebook.com/sharer/sharer.php?u='+something);

This allows user to finish the sharing in facebook confirmation window. 这允许用户在Facebook确认窗口中完成共享。 However, now I need to share an image in a similar way. 但是,现在我需要以类似方式共享图像。
I'm also using open graph protocol to make facebook display preview of my site properly. 我还使用开放图协议使Facebook正确显示我的网站预览。 But preview invoked by using proper facebook tags is useless: 但是使用适当的facebook标记调用的预览是没有用的:

在此处输入图片说明
I wonder: how can I force facebook to appear in english? 我想知道:如何强制Facebook以英文显示?

Is it possible to either share site as an image or share image by it's URL directly? 是否可以将站点共享为图像或通过URL直接共享图像?

If possible, I wan't to do without Facebook API scripts on my site, as well as any iframes or other 如果可能的话,我会在网站上没有Facebook API脚本以及任何iframe或其他 ugly 丑陋 external stuff. 外部的东西。
I also have no interest in knowing what the user did with the share window. 我也不希望知道用户对共享窗口做了什么。

You'll need the image meta tag in your page head. 您需要在页面标题中添加图片元标记。 (add your image url to the facebook og:image meta tag and so on and so forth) (将您的图片网址添加到facebook og:image元标记等)

<!-- for Google -->
<meta name="description" content="" />
<meta name="keywords" content="" />

<meta name="author" content="" />
<meta name="copyright" content="" />
<meta name="application-name" content="" />

<!-- for Facebook -->          
<meta property="og:title" content="" />
<meta property="og:type" content="article" />
<meta property="og:image" content="" />
<meta property="og:url" content="" />
<meta property="og:description" content="" />

<!-- for Twitter -->          
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="" />
<meta name="twitter:description" content="" />
<meta name="twitter:image" content="" />

Did you try using this one? 你尝试过使用这个吗?

Add this after the body 在身体后添加

    <div id="fb-root"></div>
    <!-- FB Like -->
    <script>(function(d, s, id) {
          var js, fjs = d.getElementsByTagName(s)[0];
          if (d.getElementById(id)) return;
          js = d.createElement(s); js.id = id;
          js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
          fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    </script>

and this to where your share button to appear 这到您的共享按钮出现的位置

< div class="fb-share-button" data-type="button_count"> <div class =“ fb-share-button” data-type =“ button_count”>

One way, that I can think of, is sharing an image in the form of a Facebook post. 我可以想到的一种方式是以Facebook帖子的形式共享图像。 But again, it requires the use of Facebook API . 但同样,它需要使用Facebook API Anyways, I'll just share this method here and you can use it in case you feel like using Facebook API. 无论如何,我只会在这里分享此方法,您可以在想使用Facebook API的情况下使用它。

For this you can make a POST request to the /{user-id}/feed endpoint. 为此,您可以向/{user-id}/feed端点发出POST请求。 For example: 例如:

me/feed?message="Test"&link={Image_URL}&access_token={User_Access_Token}

This method requires a valid User Access Token with publish_stream permission. 此方法需要具有publish_stream权限的有效用户访问令牌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM