apprequests object throws “An access token is required” error even when providing App Access Token

StackOverflow https://stackoverflow.com/questions/10873281

  •  12-06-2021
  •  | 
  •  

Question

I'm trying to get information regarding apprequests sent by my application (see https://developers.facebook.com/docs/reference/api/user/#apprequests), but even after providing my app access token, I'm getting this response whenever I try to access an apprequest object in the Graph API:

{
  "error": {
    "message": "An access token is required to request this resource.", 
    "type": "OAuthException", 
    "code": 104
  }
}

I retrieve my app access token by issueing a GET request to the following URL:

https://graph.facebook.com/oauth/access_token?client_id=APP_KEY&client_secret=APP_SECRET&grant_type=client_credentials

I then try to access the apprequest object by hitting

https://graph.facebook.com/REQUEST_ID?access_token=APP_ACCESS_TOKEN

When I hit this URL, I get the error as defined above. I'm providing the access_token in the URL, so this error is either a bug or I'm misunderstanding the documentation, but I think a Facebook blog post (see https://developers.facebook.com/blog/post/464/) gets apprequest information in the exact way as I have above (getting app access token, request apprequest by hitting Graph API).

Thanks for the help!

Was it helpful?

Solution

I had the same problem just now. Probably you had solve it by now, but I will reply anyway for everyone who arrives here.

If you notice the $apprequest_utl you will see that it's something like

https://graph.facebook.com/USER_ID/apprequests?message=Request_Message&data=somedata&access_token=ACCESSTOKEN&method=post

Here, what was supposed to be "&" is in fact "&", which anyway is the same but doest not work with facebook apps with php function file_get_contents. Instead, use curl and all works fine!

<?php 

function app_request ($request_string,$app_id,$app_secret,$user_id,$type) {

  $token_url = "https://graph.facebook.com/oauth/access_token?" .
    "client_id=" . $app_id .
    "&client_secret=" . $app_secret .
    "&grant_type=client_credentials";

  $app_access_token = file_get_contents($token_url);

  $request_string=utf8_encode($request_string);

  $apprequest_url ="https://graph.facebook.com/" .
    $user_id .
    "/apprequests?message=" .
    $request_string . 
    "&data=" .
    $type . 
    "&" .
    $app_access_token . "&method=post";

  $result = curl($apprequest_url);
  echo "Request id number: " . $result;

}

  function curl($url){

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        return curl_exec($curl);
        curl_close($curl);

}

//Call the function and make the app request!
app_request("Test",$APP_ID,$APP_SECRET,$USER_ID,"Test");

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top