谷歌API PHP“;无效的授权:代码已被兑换”;

谷歌API PHP“;无效的授权:代码已被兑换”;,php,google-api,google-api-php-client,Php,Google Api,Google Api Php Client,我已经尝试了这里发布的许多类似问题的答案,但似乎没有一个与我的代码一起工作。 因此,我尝试从中获取访问令牌,并尝试将其保存在数据库中 index.php <?php session_start(); require_once __DIR__ . '/vendor/autoload.php'; $client = new Google_Client(); $client->setAuthConfig('client_id.json'); $client->setAccessT

我已经尝试了这里发布的许多类似问题的答案,但似乎没有一个与我的代码一起工作。

因此,我尝试从中获取访问令牌,并尝试将其保存在数据库中

index.php

<?php

session_start();
require_once __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();
$client->setAuthConfig('client_id.json');
$client->setAccessType("offline");        // offline access
$client->addScope(Google_Service_Drive::DRIVE);


$db_host = 'localhost';

  $db_user = 'root';

  $db_password = '';

  $db_name = 'bantingdb';


$connection = @new mysqli($db_host, $db_user, $db_password, $db_name);

if($connection->connect_errno!=0)

{

  echo 'Error: '.$connection->connect_errno.' Opis: '.$connection->connect_error;



} else 

{
  $sql2 = "SELECT * FROM googledrive WHERE id_user='$_SESSION[id_user]'";

        if($query2 = @$connection->query($sql2))

        {
          $amountOfRows2 = $query2->num_rows;

          if($amountOfRows2>0)

          {

            $user = $query2->fetch_array();
          } 


        }

}

if($user['accessToken'] != '') {
  $accessToken = $user['accessToken'];
  $client->setAccessToken($access_token);
  //Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
      $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
      $accessToken = $client->getAccessToken();
      $sql = "SELECT * FROM googledrive WHERE id_user='$_SESSION[id_user]'";

          if($query = @$connection->query($sql))

          {
            $amountOfRows = $query->num_rows;

            if($amountOfRows>0)

            {
              $store = "UPDATE googledrive SET accessToken = '$accessToken' WHERE id_user='$_SESSION[id_user]'";

                    if (mysqli_multi_query($connection, $store)) 
                    {

                    }
            } else {
              $store = "INSERT INTO googledrive (id_user, accessToken) VALUES ('$_SESSION[id_user]', '$accessToken')";

                    if (mysqli_multi_query($connection, $store)) 
                    {

                    }
            }
          }
  }
  $drive_service = new Google_Service_Drive($client);
  $files_list = $drive_service->files->listFiles(array())->getFiles(); 
  if (count($files_list) == 0) {
    print "No files found.\n";
  } else {
    foreach ($files_list as $file) {
        $res['name'] = $file->getName();
        $res['id'] = $file->getId();
        $files[] = $res;
    }
    print_r($files);
  }
} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . 'callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

您应该明确添加->错误报告(E\u ALL);ini设置(“显示错误”,1);在会话开始后的页面顶部,让我们知道抛出了什么(如果有)忘记了:加上摆脱@
<?php 
session_start();
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfigFile('client_id.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/callback.php');
$client->addScope(Google_Service_Drive::DRIVE); //::DRIVE_METADATA_READONLY

if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  $client->authenticate($_GET['code']);

  $accessToken = $client->getAccessToken();

  $db_host = 'localhost';

  $db_user = 'root';

  $db_password = '';

  $db_name = 'bantingdb';


$connection = @new mysqli($db_host, $db_user, $db_password, $db_name);

if($connection->connect_errno!=0)

{

  echo 'Error: '.$connection->connect_errno.' Opis: '.$connection->connect_error;



} else 

{
   $sql = "SELECT * FROM googledrive WHERE id_user='$_SESSION[id_user]'";

         if($query = @$connection->query($sql))

    {
            $amountOfRows = $query->num_rows;

            if($amountOfRows>0)

            {
              $store = "UPDATE googledrive SET accessToken = '$accessToken' WHERE id_user='$_SESSION[id_user]'";

                    if (mysqli_multi_query($connection, $store)) 
                    {
                        $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/index.php';
                        header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
                    }
            } else {
              $store = "INSERT INTO googledrive (id_user, accessToken) VALUES ('$_SESSION[id_user]', '$accessToken')";

                    if (mysqli_multi_query($connection, $store)) 
                    {
                        $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/index.php';
                        header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
                    }
            }
          } 
   }

}