Facebook PHP SDK处理访问令牌 [英] Facebook PHP SDK dealing with Access Tokens

查看:127
本文介绍了Facebook PHP SDK处理访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经爬过了很多各种各样的答案,但仍然有点困惑,我应该如何处理Facebook 访问令牌
我遇到的主要问题之一是由于我的浏览器中存储了什么信息。例如,我登录到应用程序,令牌过期,我无法再登录,除非我清除浏览器中的cookies /应用程序设置。

I have crawled around lots of various answers but am still a bit confused with how I should be dealing with facebook access tokens. One of the main problems I'm having is due to what information is being stored in my browser. For example, I log onto the app, the token expires, I can't logon again unless I clear cookies/app settings in browser.

我偶然发现这个主题:如何从offline_access弃用后扩展访问令牌的有效性

I stumbled across this thread: How to extend access token validity since offline_access deprecation

哪个显示了如何通过php创建扩展访问令牌

Which has shown me how to create an extended access token through php.

我的问题是:

1。 >我需要在任何地方存储访问令牌吗?

1. Do I need to store the access token anywhere?

2。访问令牌到期或变得无效时会发生什么?目前,我的应用程序在短期访问期满时就会停止工作。

2. What happens when the access token expires or becomes invalid? At the moment, my app simply stops working when the short term access ones expire.

3。有没有办法处理它们来检查他们是否已经过期?
我正在使用php sdk,并基本使用标准if($ user)...像这样:

3. Is there a way I should be handling them to check if they have expired? I am using the php sdk and have basically used the standard if( $user )... Like this:

require 'sdk/src/facebook.php';

  $facebook = new Facebook(array(
  'appId'  => 'XXXXXXXXXXXXXXXXXXXXX',
  'secret' => 'XXXXXXXXXXXXXXXXXXXXX',
));

  $user = $facebook->getUser();

  if( $user ){
    try{
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
    }
  }

  if (!$user){

    $params = array(
    'scope' => 'email',
    );

    $loginUrl = $facebook->getLoginUrl( $params );
        echo '<script type="text/javascript"> 
                window.open("'. $loginUrl .'", "_self"); 
                </script>';
                exit;

 } 
     if( $user ){

    $access_token = $facebook->getExtendedAccessToken();     

     $get_user_json = "https://graph.facebook.com/me?access_token=" 
       . $access_token;

// Rest of my code here...
}



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆