Php Google API RefreshToken不适用于Google Drive

Php Google API RefreshToken不适用于Google Drive,php,laravel-5,google-drive-api,Php,Laravel 5,Google Drive Api,我想在Google Drive中创建一个文件夹,同时单击Laravel项目中的按钮。我遵循这个教程 , 下面的代码工作正常。但在1小时(3600秒)后,我无法创建文件夹,并出现以下错误 { "status": "Order not updated", "msg": "{\n \"error\": {\n \"errors\": [\n {\n \"domain\": \"global\",\n \"reason\": \"authError\",\n \

我想在Google Drive中创建一个文件夹,同时单击Laravel项目中的按钮。我遵循这个教程 ,

下面的代码工作正常。但在1小时(3600秒)后,我无法创建文件夹,并出现以下错误

{
    "status": "Order not updated",
    "msg": "{\n \"error\": {\n  \"errors\": [\n   {\n    \"domain\": \"global\",\n    \"reason\": \"authError\",\n    \"message\": \"Invalid Credentials\",\n    \"locationType\": \"header\",\n    \"location\": \"Authorization\"\n   }\n  ],\n  \"code\": 401,\n  \"message\": \"Invalid Credentials\"\n }\n}\n",
    "is_success": false
}
那么,我想再次手动创建访问令牌,然后在这里更新它。 代码如下所示

GooGleDriveController.php

<?php

namespace App\Http\Controllers;

use Exception;
use Google_Client;
use Google_Service_Drive;
use Google_Service_Drive_DriveFile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use App\Order;
use App\Enums\GoogleDriveEnum;

class GoogleDriveController extends Controller
{
    private $drive;

    public function getDrive($id)
    {
        try
        {
            $order=Order::find($id);
            $client = new Google_Client();
            $client->setClientId(GoogleDriveEnum::getValue('CLIENT_ID'));
            $client->setClientSecret(GoogleDriveEnum::getValue('CLIENT_SECRET'));
            $client->setRedirectUri(GoogleDriveEnum::getValue('REDIRECT'));
            $client->addScope(GoogleDriveEnum::getValue('SCOPE'));
            $client->setAccessToken(GoogleDriveEnum::getValue('ACCESSTOKEN'));
            $client->setIncludeGrantedScopes(true);
            $client->setApprovalPrompt("consent"); //none || consent || select_account
            $client->setAccessType("offline");
            $service = new Google_Service_Drive($client);

            $code="my_code_is_here";
            $refreshToken="my_refresh_token_is_here";
            // $client->authenticate($code);

            $tokens = $client->GetAccessToken(GoogleDriveEnum::getValue('CLIENT_ID'), GoogleDriveEnum::getValue('REDIRECT'), GoogleDriveEnum::getValue('CLIENT_SECRET'), $code);
            $client->setAccessToken($tokens["access_token"]);
            $this->drive = new Google_Service_Drive($client);

            $folder_meta = new Google_Service_Drive_DriveFile(array(
                'name' => $order->code,
                'mimeType' => 'application/vnd.google-apps.folder'));
            $folder = $this->drive->files->create($folder_meta, array(
                'fields' => 'id'));
            $order->google_drive_link = "https://drive.google.com/drive/u/0/folders/".$folder->id;
            $order->save();

            $response["msg"] = "Folder has been successfully created";
            $response["status"] = "Success";
            $response["is_success"] = true;
            return response()->json($response);

        }catch(\Exception $th)
        {
            $response["status"] = "Folder not created";
            $response["msg"] = $th->getMessage();
            $response["is_success"] = false;
            return response()->json($response);
        }
    }
}
<?php

namespace App\Enums;

use BenSampo\Enum\Enum;

final class GoogleDriveEnum extends Enum
{
    const CLIENT_ID = 'my_client_id';
    const CLIENT_SECRET = 'my_client_secret';
    const REDIRECT = 'https://developers.google.com/oauthplayground';
    const SCOPE = 'https://www.googleapis.com/auth/drive';
    const ACCESSTOKEN = 'my_access_token';
    const REFRESHTOKEN = 'my_refresh_token';
    const CODE = 'my_code';
}

我建议您使用credentials.json文件在代码中实现PHP快速启动[1]。使用访问令牌,您可以在每次过期(3600秒)时更新刷新令牌,这是一种更好的方法:

if ($client->isAccessTokenExpired()) {
    // Refresh the token if possible, else fetch a new one.
    if ($client->getRefreshToken()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    } else {
        // Request authorization from the user.
        $authUrl = $client->createAuthUrl();
        printf("Open the following link in your browser:\n%s\n", $authUrl);
        print 'Enter verification code: ';
        $authCode = trim(fgets(STDIN));

        // Exchange authorization code for an access token.
        $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
        $client->setAccessToken($accessToken);

        // Check to see if there was an error.
        if (array_key_exists('error', $accessToken)) {
            throw new Exception(join(', ', $accessToken));
        }
    }
    // Save the token to a file.
    if (!file_exists(dirname($tokenPath))) {
        mkdir(dirname($tokenPath), 0700, true);
    }
    file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}

[1]

它显示
未定义的索引:expires\u在
中,我想脱机访问。不打开浏览器进行授权。使用手动方式,您仍然需要打开浏览器才能获取令牌。如果您完全实现了快速启动,您将只需在第一次打开浏览器即可获取令牌,之后令牌将在即将过期时刷新。因此,第一次需要从浏览器登录时,无需再次登录,对吗?确切地说,它将使用下载的token.json刷新访问令牌。至少您以后会添加更多的作用域,您需要删除其中的token.json,然后再次运行代码,这将提示您在同意屏幕(登录)中创建一个新的token.json,其中包含您需要的新作用域。您还可以进行检查,您可以使用它进行API调用,而无需进行身份验证。