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

≡放荡痞女 提交于 2019-12-08 05:40:20

问题


NOTE: When the user accepts my permissions to use my app. I ask them to accept manage_notifications

I am so confused. I research this on the web and it seems I get a mix of deprecated and wrong information and I am trying to send a notification to the user using my facebook canvas app using facebook graph. the following is how I do it and its not working.

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];
}

The result passes something like this access_token=523216317ewwer235|K4A1XugBpajwrweu1K2k12jzckdU . so that is why i explode the = sign within the code. I call this function every time the user enters my app. I take the access code and pass it as $token in the following code.

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);
}

and I get the following response

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

回答1:


You must use POST requests in order to make notifications work :

curl_setopt ($c, CURLOPT_POST, true);

Apparently you are making GET requests, trying to request a resource.



来源:https://stackoverflow.com/questions/14043512/a-user-access-token-is-required-to-request-this-resource-facebook-graph-for-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!