Google API PHP“invalid_grant:代码已被兑换”

时间:2017-04-09 09:47:06

标签: 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->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));
}

callback.php

<?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));
                    }
            }
          } 
   }

}

我不断收到invalid_grant错误。

感谢。

0 个答案:

没有答案