跑道 API - 会话管理 class 访问 Redis 中的令牌时出错

Podio API - Session Management class error in accessing tokens in Redis

我正在尝试将 Session Management 用于 API 调用,因此我不会在每次我的脚本 运行 时都触发 Auth class 函数。我主要使用App ID身份验证,所以我使用了为Redis提供的示例。

但是,我遇到了一个错误 "Fatal error: Uncaught Error: Cannot access self:: when no class scope is active in /var/www/html/authcheck.php:22 Stack trace: #0 {main} thrown in /var/www/html/authcheck.php on line 22"

第22行的代码是这样的-Podio::$oauth = self::$session_manager->get(Podio::$auth_type);

这是会话管理器的 PHP 脚本 class:

文件名:SessionManager.php

<?php 
require ('podio/podio_lib/PodioAPI.php');
require ('predis/autoload.php');

class PodioRedisSession {

  /**
   * Create a pointer to Redis when constructing a new object
   */
  public function __construct() {
    $this->redis = new Predis\Client();
  }

  /**
   * Get oauth object from session, if present. We use $auth_type as
   * basis for the cache key.
   */
  public function get($auth_type = null) {

    // If no $auth_type is set, just return empty
    // since we won't be able to find anything.
    if (!$auth_type) {
      return new PodioOauth();
    }

    $cache_key = "podio_cache_".$auth_type['type']."_".$auth_type['identifier'];

    // Check if we have a stored session
    if ($this->redis->exists($cache_key)) {

      // We have a session, create new PodioOauth object and return it
      $cached_value = $this->redis->hgetall($cache_key);
      return new PodioOAuth(
        $cached_value['access_token'],
        $cached_value['refresh_token'],
        $cached_value['expires_in'],
        array("type"=>$cached_value['ref_type'], "id"=>$cached_value['ref_id'])
      );
    }

    // Else return an empty object
    return new PodioOAuth();
  }

  /**
   * Store the oauth object in the session. We ignore $auth_type since
   * it doesn't work with server-side authentication.
   */
  public function set($oauth, $auth_type = null) {
    $cache_key = "podio_cache_".$auth_type['type']."_".$auth_type['identifier'];

    // Save all properties of the oauth object in redis
    $this->redis->hmset = array(
      'access_token' => $oauth->access_token,
      'refresh_token' => $oauth->refresh_token,
      'expires_in' => $oauth->expires_in,
      'ref_type' => $oauth->ref["type"],
      'ref_id' => $oauth->ref["id"],
    );

  }
}

?>

文件名:authcheck.php

<?php

    require ('podio/podio_lib/PodioAPI.php');
    include ('SessionManager.php');
    $client_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $app_id = "xxxxxxxxxxx";
    $app_token = "xxxxxxxxxxxxxxxxxxx";



    Podio::setup($client_id, $client_secret, array(
      "session_manager" => "PodioRedisSession"
    ));

    // podio-php will attempt to find a session automatically, but will fail 
    because
    // it doesn't know which $auth_type to use.
    // So we must attempt to locate a session manually.
    Podio::$auth_type = array(
      "type" => "app",
      "identifier" => $app_id
    );
    Podio::$oauth = self::$session_manager->get(Podio::$auth_type);

    // Now we can check if anything could be found in the cache and
    // authenticate if it couldn't
    if (!Podio::is_authenticated()) {
      // No authentication found in session manager.
      // You must re-authenticate here.

      Podio::authenticate_with_app($app_id, $app_token);
    } else {

    //echo "<pre>".print_r($_SESSION, true)."</pre>";   
    echo "You already authenticated!";  

    }

    // // We can safely switch to another app now
    // // First attempt to get authentication from cache
    // // If that fails re-authenticate
    // Podio::$auth_type = array(
      // "type" => "app",
      // "identifier" => $another_app_id
    // );
    // Podio::$oauth = self::$session_manager->get(Podio::$auth_type);

    // if (!Podio::is_authenticated()) {
      // // No authentication found in session manager.
      // // You must re-authenticate here.

      // Podio::authenticate_with_app($another_app_id, $another_app_token);
    // }
    ?>

您好,我选择不使用 redis,而是使用会话和 PDO Mysql 来存储 podio 身份验证。