i have a code that uses creates a google document:
public function create_content_document(Request $request)
{
$data = $request->all();
$contentId = $data['id'];
// Convert HTML to plain text
$textContent = $data['text'];
// Create a new Google Document
$client = new Google_Client();
$client->setApplicationName('name');
$client->setScopes([
Google_Service_Docs::DOCUMENTS,
Google_Service_Drive::DRIVE_FILE,
]);
$credentialsPath = storage_path('service account creds.json');
// for debugging dd the content of the file
$client->setAuthConfig($credentialsPath);
$client->setAccessType('offline');
$service = new Google_Service_Docs($client);
// Create a new empty document with a title
$document = new Google_Service_Docs_Document([
'title' => "title",
]);
$createdDocument = $service->documents->create($document);
// Get the document ID
$documentId = $createdDocument->documentId;
// Save the document link on the content
$documentLink = "https://docs.google.com/document/d/{$documentId}";
// Set sharing settings to anyone with the link
$driveService = new Google_Service_Drive($client);
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('writer');
$driveService->permissions->create($documentId, $newPermission);
// Update the document content
$service->documents->batchUpdate(
$documentId,
new Google_Service_Docs_BatchUpdateDocumentRequest([
'requests' => [
'insertText' => [
'location' => ['index' => 1],
'text' => $textContent,
],
],
])
);
// Redirect or return the document link as needed
return $documentLink;
}
this creates the doc and saves it, but its owned by the service account, soon i want to make it so its saved in a drive folder, bun that would also mean that it'll be the service accounts folder.
so the plan is to dispose of the service account and add creds for my gmail account and have the docs appear in my drive directly.
this solution seems to be easy, just replace the service account json creds file with my own accounts json creds file, but i dont know where to generate that from.
creds screen
i tried creating a OAuth 2.0 Client key, and it seems to download a json file, but its not the same format as the service account, and it doesnt work when i add it, i get auth error.
so i guess my question is either, how to create creds for the my gmail account directly and use them to create docs sheets and drive folders, or if not available is there a way to achieve my goal?