即使提供App Access Token,apprequests对象也会抛出“需要访问令牌”错误

时间:2012-06-03 19:29:21

标签: facebook-graph-api apprequests

我正在尝试获取有关我的应用程序发送的应用程序的信息(请参阅https://developers.facebook.com/docs/reference/api/user/#apprequests),但即使提供了我的应用程序访问令牌,每当我尝试访问apprequest对象时,我都会收到此响应。图API:

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

我通过向以下网址发出GET请求来检索我的应用访问令牌:

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

然后我尝试通过点击

来访问apprequest对象

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

当我点击此网址时,我收到上面定义的错误。我在URL中提供了access_token,所以这个错误要么是错误,要么我误解了文档,但我认为Facebook博客文章(请参阅https://developers.facebook.com/blog/post/464/)以我所掌握的方式获取apprequest信息上面(获取应用访问令牌,通过点击图谱API请求apprequest)。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我刚才遇到了同样的问题。可能你现在已经解决了,但无论如何我会回复所有到达这里的人。

如果您注意到$ apprequest_utl,您会看到它类似于

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

这里应该是什么“&”实际上是“& amp;”,无论如何都是相同的,但不能与带有php函数file_get_contents的facebook应用程序一起工作。相反,使用curl,一切正常!

<?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");

?>