Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何验证Facebook应用程序以使用javascript API访问公共页面提要?_Javascript_Facebook_Facebook Graph Api_Facebook Javascript Sdk - Fatal编程技术网

如何验证Facebook应用程序以使用javascript API访问公共页面提要?

如何验证Facebook应用程序以使用javascript API访问公共页面提要?,javascript,facebook,facebook-graph-api,facebook-javascript-sdk,Javascript,Facebook,Facebook Graph Api,Facebook Javascript Sdk,我正在尝试使用facebook API访问facebook公共页面的帖子,但我似乎无法通过所有正确的凭据进行身份验证。FB.api返回: {"error":{"type":"http","message":"unknown error"}} “访问令牌1:第1行”中记录了一个错误 Uncaught ReferenceError: TRJg376YTvXfk6sMur4Mggh5YnU is not defined access_token:1 (匿名函数) 以下是我使用的代码: <sc

我正在尝试使用facebook API访问facebook公共页面的帖子,但我似乎无法通过所有正确的凭据进行身份验证。FB.api返回:

{"error":{"type":"http","message":"unknown error"}}
“访问令牌1:第1行”中记录了一个错误

Uncaught ReferenceError: TRJg376YTvXfk6sMur4Mggh5YnU is not defined access_token:1
(匿名函数)

以下是我使用的代码:

<script src="http://connect.facebook.net/en_US/all.js"></script>
<div id="fb-root"></div>
<button name="my_full_name" onclick="connectToFacebook()" value="My Name" />

<script>
function connectToFacebook() {

    FB.api('https://graph.facebook.com/oauth/access_token',
    'get',
    {client_id:'xxxxxxxxxx', client_secret:'xxxxxxxxxx',grant_type:'client_credentials'}, //client_credentials
        function(response) {
            alert(JSON.stringify(response));
        });

        FB.api("/publicpage/feed",
                function (response) {
                    console.log(response);
                });

    }
</script>

函数connectToFacebook(){
FB.api('https://graph.facebook.com/oauth/access_token',
“得到”,
{客户端id:'xxxxxxxxx',客户端机密:'xxxxxxxxx',授权类型:'client\u credentials'},//客户端\u credentials
功能(响应){
警报(JSON.stringify(响应));
});
FB.api(“/publicpage/feed”,
功能(响应){
控制台日志(响应);
});
}

他们的网站上没有很好的文档记录!有什么想法吗?

是的,文档令人困惑,但你错了,全错了。下面是Facebook Javascript SDK的基本设置

在那篇文章中,它一直在使用用户访问令牌,但当您想要访问页面的公共帖子时,您可以使用应用访问令牌。应用程序访问令牌的好处是它永远不会过期。这最终是你的选择

使用当前登录用户的访问令牌获取公共帖子

function getPosts() {           

       FB.api('/thepcwizardblog/feed', { limit: 10 }, function (response) {
            for (var i = 0, l = response.data.length; i < l; i++) {
                var post = response.data[i];
                var userid = post.from.id;

                var msg;

                if (post.message) {                        
                    msg = post.message;                        
                }
                else {                
                    msg = post.description;                        
               }

               console.log(userid + msg);
            }
        });
}
函数getPosts(){code> api('/thepcwizardblog/feed',{limit:10},函数(响应){ 对于(var i=0,l=response.data.length;i 使用应用访问令牌获取公共帖子

function getPosts() {           

       FB.api('/thepcwizardblog/feed?access_token='+accessToken+'', { limit: 10 }, function (response) {
            for (var i = 0, l = response.data.length; i < l; i++) {
                var post = response.data[i];
                var userid = post.from.id;

                var msg;

                if (post.message) {                        
                    msg = post.message;                        
                }
                else {                
                    msg = post.description;                        
               }

               console.log(userid + msg);
            }
        });
}
函数getPosts(){code> api('/thepcwizardblog/feed?access_token='+accessToken+'',{limit:10},函数(响应){ 对于(var i=0,l=response.data.length;i 您可以从获取应用程序的访问令牌

[编辑]


不要使用CBroe在评论中建议的应用程序访问令牌。

-1因为在客户端代码中公开应用程序访问令牌是一个非常愚蠢的想法。@CBroe这仍然比他现在公开
客户端秘密
要好:p,无论如何我都会编辑它!!它没有多大区别–
app_id | app_secret
用于有效的app访问令牌(或者我可以点击端点获得一个)。无论是app_secret还是app access token,都与客户端JS代码无关。@CBroe
app access token=app_id | app_secret
我总是忘记这一点,谢谢提醒!这个端点应该只被服务器端应用程序使用——因为它需要你的应用程序机密,而你不想在客户端JS代码中公开它,因为每个人都能找到它。