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


"A user access token is required to request this resource." Facebook graph for notifications

注意:当用户接受我的权限使用我的应用程序时,我要求他们接受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];
}

传递的结果类似于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);
}

得到以下响应

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

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

curl_setopt ($c, CURLOPT_POST, true);

显然你正在发出GET请求,试图请求一个资源。