从今天开始,当我尝试获取共享计数时,答案是:对于v2.9及更高版本,不推荐使用share field。
前者:https://graph.facebook.com/?id=https://stackoverflow.com&fields=share
如果没有&fields = share,则显示json内容但没有共享值。
我需要通过网址获取Facebook的份额。
API确实发生了变化。
它应该是这样的。
您需要一个访问令牌。如果您有Facebook,请访问https://developers.facebook.com/并制作应用程序。
然后单击“Graph API Explorer”。
和“获取令牌”(获取应用令牌)。而已。
如果您使用JavaScript进行计数,它将是这样的。
// split('#')[0] : Remove hash params from URL
const url = encodeURIComponent( window.location.href.split('#')[0] );
$.ajax( {
url : '//graph.facebook.com/?id=' + url + '&fields=engagement&access_token=user-access-token',
dataType : 'jsonp',
timeout: 5000,
success : function( obj ) {
let count = 0;
if ( typeof obj.engagement.reaction_count !== 'undefined' ) {
count = obj.engagement.reaction_count;
}
// do something with 'count'
},
error : function() {
// do something
}
} );
还有其他计数类型,例如comment_count和share_count。
见https://developers.facebook.com/docs/graph-api/reference/v3.2/url
有没有办法在不发送访问令牌的情况下接收计数?
我想知道我自己哈哈
由于您无法在前端显示访问令牌,我建议您使用nginx代理请求,隐藏服务器上的access_token。
http {
...
# Optional: set facebook cache zone
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=facebook:100m inactive=60m;
...
}
server {
server_name example.org;
...
location /facebook {
# Optional: don't log requests
access_log off;
log_not_found off;
# Allow get shares only for single domain (remove condition to allow all domains)
if ( $arg_id ~ "^https://example.org/" ) {
set $args"${args}&access_token=your_access_token_here";
}
# Set dns resolver address (you can change it with any dns server)
resolver 1.1.1.1;
proxy_pass https://graph.facebook.com?$args;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Optional: add cache for 30 minutes
proxy_ignore_headers Expires;
proxy_ignore_headers Cache-Control;
proxy_cache facebook;
proxy_cache_valid any 30m;
proxy_cache_key $host$uri$is_args$arg_id;
}
...
}
之前:
后:
https://example.org/facebook?fields=engagement&callback=FB.Share&id=https://example.org/