I want to create a folder in Google Drive with the given code while click the button in the Laravel project. I am following this tutorial API Keys, Refresh Token
I works fine. But after 1 hour(3600 seconds) I am unable to create the the folder and I got the following error.
{
"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
}
So, then I want to create the key again manually then update it here.
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);
}
}
}
GoogleDriveEnum.php
<?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';
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire