【问题标题】:PHP + Facebook: how to get access_token + 60 day long access token?PHP + Facebook:如何获取 access_token + 60 天访问令牌?
【发布时间】:2012-11-04 02:53:09
【问题描述】:

我正在尝试这样:

https://graph.facebook.com/oauth/access_token?             
    client_id=APP_ID&
    client_secret=APP_SECRET&
    grant_type=fb_exchange_token&
    fb_exchange_token=EXISTING_ACCESS_TOKEN 

url地址中的所有变量都设置正确,但是当我把这个url放入浏览器时,我得到:

{
   "error": {
      "message": "No user access token specified",
      "type": "OAuthException",
      "code": 1
   }
}

一遍又一遍。我做错了什么?

编辑:

我目前的设置: 脚本地址:www.domain.com/folder/index.php

$my_url = "www.domain.com"

在脸书上: namespace: my_namespace

应用域:domain.com

网站网址http://www.domain.com/

这是我的设置,但仍然出现同样的错误

【问题讨论】:

    标签: php facebook facebook-graph-api access-token facebook-access-token


    【解决方案1】:

    您是否向他们发送现有的访问令牌?您首先应该有一个 access_token,您应该将其发回以获取扩展的到期访问令牌。

    从错误中,我可以理解的是,您没有向他们发送令牌。

    这是我在其他问题中回答的代码。它在 FB 文档中明确给出。这个获取短期令牌,将其发送回以扩展它,并获取长期访问令牌。

    <?php
    
    //read more : https://developers.facebook.com/docs/howtos/login/server-side-login/
    session_start();
       $app_id = "xxxxxxxxxxxxxx";
       $app_secret = "xxxxxxxxxxxxxxxx";
       $my_url = "www.stackoverflow.com/";  // redirect url
    
        $code = $_REQUEST["code"];
    
       if(empty($code)) {
         // Redirect to Login Dialog
         $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
         $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
           . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
           . $_SESSION['state'] . "&scope=publish_stream,read_friendlists,email";
    
    
         echo("<script> top.location.href='" . $dialog_url . "'</script>");
       }
    if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
         $token_url = "https://graph.facebook.com/oauth/access_token?"
           . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
           . "&client_secret=" . $app_secret . "&code=" . $code;
    
         $response = file_get_contents($token_url);
         $params = null;
         parse_str($response, $params);
         $longtoken=$params['access_token'];
    
    
    //save it to database    
    ?>
    

    【讨论】:

    • 感谢 Kishor 的回答。我试过你的代码,但我对$my_url 有疑问。 facebook 开发者部分中的这个 URL 是什么?假设我在domain.com/my_addr/script_which_contains_the_code_above.php 中有应用程序,$my_url 中应该是什么?谢谢
    • 应该是这样的,这个脚本应该在用户进入的登陆页面中。所以他们被用于oauth并且你得到一个访问令牌。他们被重定向到您希望他们访问的站点。那应该是 $my_url。
    • Kishor,请看一下更新的 OP。因为我现在有点迷茫,所以我添加了我当前的设置。
    • 还是一样:API Error Code: 191 - API Error Description: The specified URL is not owned by the application - Error Message: redirect_uri isn't an absolute URI. Check RFC 3986.
    • 它在文档中的某处明确指出,“新”/延长的身份验证令牌可能与以前/现有的相同,但没有成为。所以我会小心依赖这种情况。
    【解决方案2】:

    您不能将应用令牌放在那里。它必须是 User 访问令牌。请使用现有的用户访问令牌。

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      相关资源
      最近更新 更多