"请求此资源需要用户访问令牌." Facebook图表的通知

num*_*l25 1 php facebook facebook-graph-api facebook-canvas

注意:当用户接受我使用我的应用程序的权限时.我请他们接受manage_notifications

我感到很困惑.我在网上研究这个,似乎我得到了不赞成和错误信息的混合,我试图使用facebook图表使用我的Facebook画布应用程序向用户发送通知.以下是我如何做,它不起作用.

public function getAccessToken() {
    $app_id = $this->settings['app_id'];
    $app_secrete = $this->settings['app_secrete'];
    $url =  "https://graph.facebook.com/oauth/access_token?".
            "client_id={$app_id}".
            "&client_secret={$app_secrete}".
            "&grant_type=client_credentials";
    $c = curl_init($url);
    // necessary so CURL doesn't dump the results on your page
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($c);
    $result = explode("=", $result);
    curl_close ($c);
    return $result[1];
}
Run Code Online (Sandbox Code Playgroud)

结果通过这样的事情 access_token=523216317ewwer235|K4A1XugBpajwrweu1K2k12jzckdU.所以这就是我=在代码中爆炸标志的原因.每次用户进入我的应用程序时,我都会调用此函数.我获取访问代码并将其传递,如$token下面的代码所示.

public function alertUser($userid,$token,$message="You have a notification") {
    $inviteMessage = $message;
    $inviteMessage = urlencode($inviteMessage);
    $url = "https://graph.facebook.com/{$userid}/notifications?access_token={$token}&".
    "template={$inviteMessage}&ref=notify";
    $c = curl_init($url);
    // necessary so CURL doesn't dump the results on your page
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($c);
    curl_close ($c);        
    $r = json_decode($result);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下回复

object(stdClass) {
    error => object(stdClass) {
        message => 'A user access token is required to request this resource.'
        type => 'OAuthException'
        code => (int) 102
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

您必须使用POST请求才能使通知工作:

curl_setopt ($c, CURLOPT_POST, true);

显然你正在发出GET请求,试图请求资源.