Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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
Javascript 无法获取Facebook访问令牌_Javascript_Php_Facebook_Facebook Graph Api - Fatal编程技术网

Javascript 无法获取Facebook访问令牌

Javascript 无法获取Facebook访问令牌,javascript,php,facebook,facebook-graph-api,Javascript,Php,Facebook,Facebook Graph Api,我有两个页面,facebook.php和generatePageToken.php,我正试图以页面的形式发布到我的facebook页面,但这个脚本目前仅以管理员的身份发布。在facebook.php页面中,我尝试获取一个访问令牌,但它不起作用——它会发出空白警报。请不要告诉我阅读一些API,我已经阅读了所有的内容,我真的不明白这是怎么回事。如果有人能挥动它,我想要一个永久的代币 facebook.php <!doctype html> <html> <head>

我有两个页面,facebook.php和generatePageToken.php,我正试图以页面的形式发布到我的facebook页面,但这个脚本目前仅以管理员的身份发布。在facebook.php页面中,我尝试获取一个访问令牌,但它不起作用——它会发出空白警报。请不要告诉我阅读一些API,我已经阅读了所有的内容,我真的不明白这是怎么回事。如果有人能挥动它,我想要一个永久的代币

facebook.php

<!doctype html>
<html>
<head>
        <script type="text/javascript">

        function SendToFacebook()
        {
            window.fbAsyncInit = function () {
                // init the FB JS SDK
                FB.init({
                    appId: '432036336937975',                        // App ID from the app dashboard
                    status: false,                                 // Check Facebook Login status
                    xfbml: true                                  // Look for social plugins on the page
                });


                FB.login(function (response) {
                    FB.api('/me/accounts', function (apiresponse) {
if (response.authResponse) {
        //simple user access token
        var accessToken = response.authResponse.accessToken,
            ajaxRequest = new XMLHttpRequest(),
            pageId = '1521533601413118';

        ajaxRequest.onreadystatechange = function() {
            if(ajaxRequest.readyState === 4) {
                //print out the extended page access token
                alert(ajaxRequest.responseText);
            }
        };
        ajaxRequest.open('POST','generatePageToken.php?pageId=' + pageId, true);
        ajaxRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        ajaxRequest.send('accessToken=' + accessToken);
    }

                        var data = {
                            message: "mymessage test",
                            display: 'iframe',
                            caption: "caption",
                            name: "name",
                            description: "description",
                            to: '1521533601413118',
                            from: '1521533601413118'
                        };

                        FB.api('/1521533601413118/feed', 'post', data, function () {
                            console.log(arguments);
                        });


                    });

                }, { scope: 'manage_pages'});

            };
            // Load the SDK asynchronously
            (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";
                fjs.parentNode.insertBefore(js, fjs);
            } (document, 'script', 'facebook-jssdk'));
        }
        </script>

</head>
<body>
<a href="#" onclick="SendToFacebook();">Submit</a>
</body>
</html>
generatePageToken.php

<?php
$accessToken = $_GET['accessToken'];
$pageId = $_POST['pageId'];
$fbAppId = '432036336937975';
$fbAppSecret = 'REMOVED FOR NOW';

$appsecretProof = hash_hmac('sha256', $accessToken, $fbAppSecret);
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php-3.2');

//get extended user access token
$url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token' .
    '&client_id=' . $fbAppId .
    '&client_secret=' . $fbAppSecret .
    '&fb_exchange_token=' . $accessToken .
    '&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
$response_params = array();
parse_str($curlResult, $response_params);
$extendedUserToken = $response_params['access_token'];

$appsecretProof = hash_hmac('sha256', $extendedUserToken, $fbAppSecret);
//get extended page access token
$url = 'https://graph.facebook.com/' . $pageId .
    '?fields=access_token' .
    '&access_token=' . $extendedUserToken .
    '&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
curl_close($ch);
$pageToken = json_decode($curlResult)->access_token;

echo $pageToken;
?>

我想这是因为你把$u POST和$u GET弄混了:

您的代码:

ajaxRequest.open('POST','generatePageToken.php?pageId=' + pageId, true);
                                               ^ pageId is GET
ajaxRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
ajaxRequest.send('accessToken=' + accessToken);
                  ^ accessToken is POST
您将pageId作为GET变量发送,而accessToken是POST变量

但是,PHP检索变量的方式如下:

$accessToken = $_GET['accessToken'];
$pageId = $_POST['pageId'];
应该是另一种方式,例如:

$accessToken = $_POST['accessToken'];
$pageId = $_GET['pageId'];

如果要以页面用户的身份在页面上发布,则需要首先更改权限的范围:

您需要},{scope:'manage_pages,publish_stream'};,添加了发布流

现在,发布非常简单,只需发送一个post请求:

$data = http_build_query(array(
    'access_token'  => **your access token**,
    'message'       => 'hello world!'
));
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.1/1521533601413118/feed');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);

非常感谢。很好。现在,我的警报返回一个访问代码。然而,该测试帖子仍然是作为管理员而不是页面发布的。还有什么建议吗?我该怎么办?谢谢你,戴夫。不确定如何使用此访问代码发布为页面我相信您正在寻找。有了它,您就可以准确地测试您想要用您的访问令牌检索什么。例如,使用此URL,https://graph.facebook.com/v2.1/me?access_token=***&fields=id%2Cname&format=json&method=get&pretty=0&suppress\u http\u code=1,用访问令牌填充星星,将允许我获取用户的id和名称。我只想在我的页面上发布一条消息,作为我的页面。我将如何将其纳入?我不想弄乱我的代码,冒着把事情搞砸的风险。如果你想以页面所有者/管理员的身份在页面上发布,那么为什么这个脚本目前只以管理员的身份发布?对其进行自我验证的用户将是正在发布的用户。您只需以页面管理员的身份登录,保存令牌,并将其硬编码到您的代码中,就可以避免将用户名和密码硬编码到PHP脚本中。不,我不想以管理员/所有者的身份发布。我想成为这一页。例如,我的名字是Ryan Reese。我的页面是CodeFundamentals。我目前的代码只是以Ryan Reese的身份发布。我想把它作为CodeFundamenatls我管理这个页面张贴