找不到“Silex \ Application”类

时间:2017-01-26 06:43:55

标签: php silex

我已完成谷歌注册,我想问一下谷歌token_id身份验证。 Google向每个登录时发生更改的用户发出token-id,当用户登录时,我会获得该令牌ID,我想通过Google验证该令牌ID以验证是否已登录是原始的还是假的。我正在使用谷歌提供的这个php api,但它不断给出这个错误:

  

未捕获错误:在C:\ xampp \ htdocs \ final \ gplus-verifytoken-php-master \ verify.php中找不到类'Silex \ Application':23

     

堆栈跟踪:在第23行的C:\ xampp \ htdocs \ final \ gplus-verifytoken-php-master \ verify.php中抛出#0 {main}

require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/google-api-php-client/src/Google_Client.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

const CLIENT_ID = 'xyz';   
const CLIENT_SECRET = 'xyz';
const APPLICATION_NAME = "xyz";

$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);

$app = new Silex\Application();
$app['debug'] = true;

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__,
));
$app->register(new Silex\Provider\SessionServiceProvider());

// Initialize a session for the current user, and render index.html.
$app->get('/', function () use ($app) {
    return $app['twig']->render('index.html', array(
        'CLIENT_ID' => CLIENT_ID,
        'APPLICATION_NAME' => APPLICATION_NAME
    ));
});

// Verify an ID Token or an Access Token.
// Example URI: /verify?id_token=...&access_token=...
$app->post('/verify', function (Request $request) use($app, $client) {

    $id_token = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImE0MzY0YjVmYjliODYxYzNhYTRkYTg5NWExMjk5NzZjMjgyZGJmYzIifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiaWF0IjoxNDg1NDEyMjQ1LCJleHAiOjE0ODU0MTU4NDUsImF0X2hhc2giOiJMSV9DTWxzeG1lSTdvQm9lSUxoSjZRIiwiYXVkIjoiNDY4MzU1OTM0NzMzLXZqNnRkdDJtazEwZ3R0OHJvZGY2bG84MHM4czdtdTRrLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTEyNjE1NTE5MDY0MTc3ODI0NTgzIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF6cCI6IjQ2ODM1NTkzNDczMy12ajZ0ZHQybWsxMGd0dDhyb2RmNmxvODBzOHM3bXU0ay5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImVtYWlsIjoibWdoYXphbmZhcmFsaWtoYW4wOUBnbWFpbC5jb20ifQ.Bpa2_zeVebQ7xtKXvuEell50bvUtKOGb5ZertUZGvzGWXnlA-c2kw4Mvko9Xd4JI_R4wbFoyBtrGCiK0jAlJMgaIH8p3wJbzNKPZ-gPFJdX8mv4v42v8-9urGM7rRUCDylz16WEcR1A2qOmEcNCpCf0_FGNpChl8sc8q8zvTnIb_zYYHp_V7ebR2RlUuO2z9G5YzBN3hZDnmen1xLStmNmYKsIiP5ypMqbWaLjnXJjre6bjTuIGymg_phDYDmwWMVTJyx88zmKAfwQTCh2u3qe_fkCDxxm0MO2wC29__q4uc0BfUNdH62GOrNTBJXmPTUZuT1vdUhzz4CLu1KUohWg";
    /*$id_token = $request->get("id_token");*/
    $access_token = $request->get("access_token");

    $token_status = Array();

    $id_status = Array();
    if (!empty($id_token)) {
      // Check that the ID Token is valid.
      try {
        // Client library can verify the ID token.
        $jwt = $client->verifyIdToken($id_token, CLIENT_ID)->getAttributes();
        $gplus_id = $jwt["payload"]["sub"];

        $id_status["valid"] = true;
        $id_status["gplus_id"] = $gplus_id;
        $id_status["message"] = "ID Token is valid.";
      } catch (Google_AuthException $e) {
        $id_status["valid"] = false;
        $id_status["gplus_id"] = NULL;
        $id_status["message"] = "Invalid ID Token.";
      }
      $token_status["id_token_status"] = $id_status;
    }

    $access_status = Array();
    if (!empty($access_token)) {
      $access_status["valid"] = false;
      $access_status["gplus_id"] = NULL;
      // Check that the Access Token is valid.
      $reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' .
              $access_token;
      $req = new Google_HttpRequest($reqUrl);

      $tokenInfo = json_decode(
          $client::getIo()->authenticatedRequest($req)
              ->getResponseBody());

      if ($tokenInfo->error) {
        // This is not a valid token.
        $access_status["message"] = "Invalid Access Token.";
      } else if ($tokenInfo->audience != CLIENT_ID) {
        // This is not meant for this app. It is VERY important to check
        // the client ID in order to prevent man-in-the-middle attacks.
        $access_status["message"] = "Access Token not meant for this app.";
      } else {
        $access_status["valid"] = true;
        $access_status["gplus_id"] = $tokenInfo->user_id;
        $access_status["message"] = "Access Token is valid.";
      }
      $token_status["access_token_status"] = $access_status;
    }

    return $app->json($token_status, 200);
});

$app->run();

2 个答案:

答案 0 :(得分:0)

我确实喜欢这个

composer install (after that run this command)
composer dump-autoload

这对我有用

答案 1 :(得分:0)

如果您的框架在迁移后无法使用。 或参见找不到“ Silex \ Application”类。 安装作曲家后删除“ vendor”文件夹。

为我工作