Lor*_*nzo 2 facebook facebook-graph-api
从今天开始,当我尝试获取共享计数时,答案是:对于v2.9及更高版本,不建议使用共享字段。
例如:https : //graph.facebook.com/?id=https : //stackoverflow.com&fields=share
如果没有&fields = share,则显示json内容,但没有共享值。
我需要从URL获取Facebook的共享计数。
qui*_*ick 10
如果您不想使用访问令牌或 nginx 代理解决方案,请参阅/sf/answers/3205785481/:
您可以使用查询
https://graph.facebook.com?id=<your-url>&fields=og_object{engagement}
Run Code Online (Sandbox Code Playgroud)
答案将是
{
"og_object": {
"engagement": {
"count": 197,
"social_sentence": "197 people like this."
},
"id": "895062470590407"
},
"id": "<your-url>"
}
Run Code Online (Sandbox Code Playgroud)
2021 年更新:您需要此请求的访问令牌。您可以在Graph API Explorer 中获取临时访问令牌或使用您的自定义应用程序生成它
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
}
} );
Run Code Online (Sandbox Code Playgroud)
还有其他计数类型,例如comment_count和share_count。
参见https://developers.facebook.com/docs/graph-api/reference/v3.2/url
有什么方法可以在不发送访问令牌的情况下接收计数?
我想知道我自己
更新:
感谢安东·卢金。
是的 我不应该显示访问令牌。它必须是隐藏的。我觉得很愚蠢。
因此,现在快速解答。没有令牌,这确实有效!
我的最终答案(希望是最终答案)是这样的。
// split('#')[0] : Remove hash params from URL
const url = encodeURIComponent( window.location.href.split('#')[0] );
$.ajax( {
url: '//graph.facebook.com/?id=' + url + '&fields=og_object{engagement}',
dataType : 'jsonp',
timeout: 5000,
success : function( obj ) {
let count = 0;
try {
count = obj.og_object.engagement.count
} catch (e) {
console.log(e)
}
// do something with 'count'
},
error : function() {
// do something
}
} );
Run Code Online (Sandbox Code Playgroud)
这里的一点是,当没有人共享目标页面时,甚至都没有定义“ og_object.engagement”。
我以为我会得到0作为回报。但是事实并非如此。
因此,让我们使用try-catch。
现在,我唯一关心的是API限制。如果您的网站获得了很多浏览量,则此更新版本可能无法正常工作。
小智 5
由于您无法在前端显示您的访问令牌,我建议您使用 nginx 代理请求,将您的 access_token 隐藏在您的服务器上。
您需要一个访问令牌。导航到https://developers.facebook.com/并制作一个应用程序。
将自定义规则添加到您的 nginx 配置
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;
}
...
}
Run Code Online (Sandbox Code Playgroud)
之前:
之后:
https://example.org/facebook?fields=engagement&callback=FB.Share&id=https://example.org/
| 归档时间: |
|
| 查看次数: |
2752 次 |
| 最近记录: |