douyun3887 2017-04-09 09:47
浏览 25

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

I have tried many answers of similar question posted here but none of them seemed to be working with my code.

So I am trying to get the access token from and trying to save it in the database.

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.
";
  } 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));
                    }
            }
          } 
   }

}

I keep getting invalid_grant error.

Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法