【问题标题】:Access token in my db我的数据库中的访问令牌
【发布时间】:2013-03-05 02:55:00
【问题描述】:

所以如果我想用过期的访问令牌捕获错误,我必须检查一下 https://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/ 好的,但它有点不清楚。

  // known valid access token stored in a database 
  $access_token = "YOUR_STORED_ACCESS_TOKEN";

所以我必须在我的数据库中保留最后一个用户访问令牌?

  $code = $_REQUEST["code"];

  // If we get a code, it means that we have re-authed the user 
  //and can get a valid access_token. 
  if (isset($code)) {

所以如果我得到$_REQUEST["code"] 这意味着用户有访问令牌?

我的应用打印用户喜欢的内容,如果用户更改密码,我必须获取新的访问令牌。我应该如何验证这些令牌?在这种情况下,Facebook 文档对我来说真的不清楚;/

【问题讨论】:

    标签: php facebook access-token


    【解决方案1】:

    您可以通过异常处理来做到这一点。如果您的访问令牌已过期或无效,它将向您抛出错误并为您提供下面提到的消息。

    public function getUserInfo($access_token)
        {    
                   try {
                            $user_info = $this->facebook->api(
                           '/me',
                           'GET',
                                   array('access_token' =>$access_token)
                        );
                        } 
                        catch(Exception $e){
                            $message = $e->getMessage();
                                if ((strpos($message, 'Error validating access token') !== false) ||
                                    (strpos($message, 'Invalid OAuth access token') !== false) ||
                                    (strpos($message, 'An active access token must be used') !== false)
                                ) {
                                    echo 'Your custom Message';
                                }
                        }
    }
    

    您可以将访问令牌传递给您的函数,然后像上面的异常处理那样检查。

    希望对你有帮助。

    通过这种方式可以获得扩展访问令牌

     /**
         * Getting User Acess Tocken , extended it and save it in to database......
         */
        public function getAccessToken($user_id,$fb_account_id)
        {
             $access_token=$this->facebook->getAccessToken();               
             $extended_access_token=$this->getExtendedAccessToken($access_token);                
            /** 
             * To save Access tocken and other necessary option
             */  
            $usr_bus_model = new Application_Model_UserBusinessAccounts;                
            $usr_bus_model->loadAccount($user_id,'Facebook',(int)$fb_account_id,false);         
            $usr_bus_model->details=$extended_access_token;
                    if(!empty($extended_access_token))
                    {
                        $usr_bus_model->status= 'active';
                    }
            $usr_bus_model->save();
    
            return $extended_access_token;
        }
    
        public function getExtendedAccessToken($access_token)
            {       
    
                $token_url="https://graph.facebook.com/oauth/access_token";
                $params=array('client_id'=>self :: appId,'client_secret'=>self :: appSecretId,'grant_type'=>'fb_exchange_token','fb_exchange_token'=>$access_token);
    
                  $response = $this->curl($token_url,$params);
                  $response = explode ('=',$response);
                  $response = explode ('&',$response[1]);
                  $response = $response[0];       
                  return $response;
    
            }
    

    我已经做了以上两个功能。因此,每当您需要使用访问令牌时,只需调用扩展访问令牌并将其保存到您的数据库中。所以你的访问令牌永远不会过期。

    【讨论】:

    • 非常感谢,可以理解了。所以如果我发现一些错误,我必须获得新的访问令牌。如果我想在墙上贴一些东西,我需要访问令牌,那么我必须将令牌保存在我的数据库中,是的,或者我应该始终将访问令牌保存在我的数据库中?
    • 是的,如果你发现了一些错误而不是你需要获取新的访问令牌。而不是将其保存到您的数据库中,只需用旧的替换即可。就是这样..我做了和上面一样的方法..
    • 最好在我的数据库中保留一个 60 天的令牌,对吗?那么当用户登录时,立即获得新的 60days 令牌?
    • 我刚刚编辑了问题。使用这种方式,您的访问令牌将永不过期。
    • 那么我应该在 base_facebook 中重新声明方法吗?
    猜你喜欢
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2012-07-14
    相关资源
    最近更新 更多