Exporting Large Google Files Via API - php

I'd like to know how to perform a large Google Slide export via API. I've followed the instructions here and it works fine with small files. If I try it with a large file, it just doesn't work. It is not returning errors or anything. The logic I'm using is the following:
<?php session_start();
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
//INCLUDE PHP CLIENT LIBRARY
require_once 'vendor/autoload.php';
// Create client object
$client = new Google_Client();
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/main.php');
$client->setAuthConfig("client_secret.json");
$client->addScope(array('https://www.googleapis.com/auth/drive'));
$client->setAccessType('offline');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Drive($client);
$pptFile = fopen("testPresentation.pptx", "w+");
$fileId = '1fx2uqe96NgsOabAv8S_mItlEbJ_NJSORqPitH55TMc';
try {
$file = $service->files->export($fileId,"application/vnd.openxmlformats-officedocument.presentationml.presentation", array("alt" => "media"));
$fileContent = $file->getBody()->getContents();
fwrite($outHandle, $fileContent);
} catch(Exception $e) {
print $e->getMessage();
}
//Close output file handle.
fclose($outHandle);
echo "Done.\n";
} else {
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/main.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
Like I said, it works with small files but not with large ones. One thing I've noticed is that when I do a var_dump($file); on the small file, the 'Content-Length' header has a size of "30083".
If I do the same on the large file, the 'Content-Length' header has a size of "0". I assume that exporting large files must be done different. I even run into this https://github.com/google/google-api-php-client/issues/1124 and tried the snippet there but had no success.

Related

Google People Api not working for web application

I have successfully managed to get the contacts on CLI as the documentation was perfect. I wanted to implement the same thing for the web browser but it is not working and repeating the google auth/consent screens again and again. I couldn't find any clue from the documentation because there was no proper example given to implement people API for the web application. The code which tried to develop at my own is given below
index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Google\Client;
use Google\Service\PeopleService;
session_start();
$client = new Google\Client();
$client->setAuthConfig('credentials.json');
$client->addScope(Google\Service\PeopleService::CONTACTS);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$service = new PeopleService($client);
$optParams = array(
'pageSize' => 10,
'personFields' => 'names,emailAddresses',
);
$results = $service->people_connections->listPeopleConnections('people/me', $optParams);
echo json_encode($results);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/gc-web/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
oath2callback.php
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google\Client();
$client->setAuthConfigFile('credentials.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/gc-web/oauth2callback.php');
$client->addScope(Google\Service\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']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/gc-web';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

Using Google Drive API with PHP on website shows not verified message

I'm checking Google Drive API docs because I want to use it in the browser and not in command line, so, according to examples, my code is like this:
index.php
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('11*****.apps.googleusercontent.com_client_secret.json');
$client->addScope(Google_Service_Drive::DRIVE);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$drive = new Google_Service_Drive($client);
$files = $drive->files->listFiles(array())->getFiles();
echo json_encode($files);
} else {
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
oauth2callback.php
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('11*****.apps.googleusercontent.com_client_secret.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/drive/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
But when I execute index.php in the browser and after login with my google account I receive this message:
How can I avoid this message?
You need to go through verification before you launch a user-facing app. You can continue to build and test your application while waiting to complete verification. When your app is successfully verified, the unverified app screen will be removed from your client.
You can follow the steps of verification for apps here.

Google Drive API Code Authentication returns 500 error

Currently, I am working on We application in PHP, which will get Google Drive files and perform some operation on it.
I have created Google Project with name My Project, with credentials which allow me to download the credential json file. I have Used following two files as mentioned in the google documentation
https://developers.google.com/api-client-library/php/auth/web-app
index.php
<?php
require 'vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('client_secret_new.json');
//$client->setAccessType("online"); // offline access
//$client->setIncludeGrantedScopes(true); // incremental auth
$client->addScope(Google_Service_Drive::DRIVE);
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/google-
drive/oauth2callback.php');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$drive = new Google_Service_Drive($client);
$optParams = array(
'fields' => 'nextPageToken, files(id, name)'
);
$files = $drive->files->listFiles($optParams);
if (count($files->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($files->getFiles() as $file) {
echo $file->getName()." ".$file->getId()."<br/>";
}
}
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google-
drive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
oauth2callback.php
<?php
require_once 'vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secret_new.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/google-
drive/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE);
if (!isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google-drive/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
Once I run index.php file, it as me to login to google account and ask for required permission, once i access the permission it moves to redirect url which is oauth2callback.php file with code in url, but oauth2callback.php once it authenticate code it gives 500 error in line $client->authenticate($_GET['code']);
please give me solution for the same.

Google API client authorization: Internal Server Error 500

I'm using an OAuth 2.0 for Web Server Applications example code on remote web server.
$client->authenticate(...) causes
Internal Server Error 500.
No idea why, can you help me please?
Here is the web address: http://imperiousgroup.com/gdrive/
And here is the code:
//index.php
require_once(__DIR__ ."/api/vendor/autoload.php");
session_start();
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$drive_service = new Google_Service_Drive($client);
$files_list = $drive_service->files->listFiles(array())->getItems();
echo json_encode($files_list);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/gdrive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
//oauth2callback.php
require_once __DIR__ .'/api/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/gdrive/oauth2callback.php');
$client->addScope(Google_Service_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']);
}
Which PHP version are you using?
I had a similar issue as I was using PHP 5.3 while the google api technically requires 5.4+.
If updating it is not an option, I was able to fix my specific issue by using the solution described here:
SO link
The call to curl_reset() is found in /vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php in the google api client folder.

Getting duplicate files while trying to upload CSV files to google spreadsheet

I have applied file insertion code i.e to upload csv files to the google drive. All is working perfectly but I am getting duplicate files instead of getting one at a time. I have to upload two different files at a time but I am getting 4 files, need help in this case
This is google.php file where I have included "Google_Client.php","Google_DriveService.php" and file "insert.php" for the insertion in google drive which contain function insertFile($service, $title, $description, $parentId, $mimeType, $filename)
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require 'insert.php';
session_start();
$title = 'Total Likes/Reblogs';
$description = 'Total Likes/Reblogs';
$title1 = 'Total Likes/Reblogs per post';
$description1 = 'Total Likes/Reblogs per post';
$parentId = '';
$mimeType = 'text/csv';
$filename = 'total.csv';
$fileNamePerPost = 'totalPerPost.csv';
$client = new Google_Client();
$client->setApplicationName("Google+ PHP Starter Application");
$service = new Google_DriveService($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
insertFile($service, $title, $description, $parentId, $mimeType, $filename);
insertFile($service, $title1, $description1, $parentId, $mimeType, $fileNamePerPost);
} else {
$state = mt_rand();
$client->setState($state);
echo $_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
Once OAuth 2.0 callback is handled, you need to stop executing. header(...) doesn't stop the script and inserts 2 files. Once the first execution is finished, redirection inserts the same files again.

Categories