Google drive Upload returns - (403) Insufficient Permission - google-drive-api

I am trying to upload files to google drive using service account, but i am getting error like
(403) Insufficient Permission error
Although fetching list of files from drive is working fine.
require_once 'google-api-php-client-master/src/Google/autoload.php';
require_once 'google-api-php-client-master/src/Google/Service.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$service_account_name = '251181790499-1en2vs6jjovga1egu15tphm1m7g44m60#developer.gserviceaccount.com';
$key_file_location = 'key.p12'; //key.p12
$key = file_get_contents($key_file_location);;
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
//$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
//$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/drive'),
$key);
$client->setAssertionCredentials($cred);
$client->setAccessType("offline");
$file = new Google_Service_Drive_DriveFile();
$file->setTitle(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');
$data = file_get_contents('a.jpg');
$service = new Google_Service_Drive($client);
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'
));
print_r($createdFile);
}
else
{
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/googledrive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
I have reviewed similar questions in forum and tried suggestions but nothing seems to work.
Is there anything wrong with code?

Change your Google Drive Scope to
Google_Service_Drive::DRIVE
in PHP
$client->addScope(Google_Service_Drive::DRIVE);

Related

List of files from my google drive

I am writing PHP script which get files from my Google Drive folder and write list of them with download links on my website, but can not find solution which take files from my folder. Every solution I found need sign up from user, that is viewing page and get list of files from his google drive. Can anyone help me with solution?
Here is my code.
function printFilesInFolder($service, $folderId) {
$pageToken = NULL;
do {
try {
$parameters = array();
$parameters = array(
'q' => "'0B5NCdsbrL1VfQ00xY3pFS3BtOE0' in parents"
);
$children = $service->files->listFiles($parameters);
var_dump($children);
foreach ($children->getFiles() as $child) {
print 'File Id: ' . $child->getId();
}
$pageToken = $children->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
}
$client = new Google_Client();
$client->setApplicationName("Web client 1");
$client->setClientId('CLIENTID');
$client->setClientSecret('SECRET');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setRedirectUri('http://localhost/googledrive/drive/drive.php');
if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
} else
$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Drive($client);
$ret = printFilesInFolder($service,"FOLDERID");
} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit();
}
But i still dont getting right data.
Second think is, that i want to remove need of authentication on every refresh of page, but i dont know how to do that.

creation a document to Google Drive and change its paper size

I use the PHP library to upload and update an HTML file and convert it to native Google document
include_once ROOT.'/google-api-php-client/vendor/autoload.php';
$client = new Google_Client();
$credentialsFile = ROOT.'/google-api-php-client/service_account.json';
if (!file_exists($credentialsFile)) {
throw new RuntimeException('Service account credentials Not Found!');
}
$client->setAuthConfig($credentialsFile);
$client->setScopes(Google_Service_Drive::DRIVE);
$service = new Google_Service_Drive($client);
/**
* Проверка существования папки для документов
*/
$optParams = array(
'q' => "name='Документы'"
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
exit('Нет папки для документов');
} else {
foreach ($results->getFiles() as $file) {
$papka_doc = $file->getId();
}
}
/**
* Обращение к шаблону
*/
$optParams = array(
'q' => "name='{$tpl_name}' "
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
exit('Нет шаблона');
} else {
foreach ($results->getFiles() as $file) {
$sh_id = $file->getId();
}
}
/**
* Получение контента из шаблона
*/
$content = $service->files->export($sh_id, 'text/html', array(
'alt' => 'media' ));
$dd =$content->getBody();
$chto = array_keys ($replace);
$chto= $this->translit($chto);
$nachto =array_values ($replace);
$nachto= $this->translit($nachto);
$dd = str_replace($chto, $nachto, $dd);
/**
* Создание основы для документа
*/
$fileId = $sh_id;
$doc_name = $tpl_name.'_'.date('dmY-Hi');
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $doc_name,
'parents' => array($papka_doc)));
$file = $service->files->copy($fileId, $fileMetadata, array(
'fields' => 'id'));
$new_file_id = $file->id;
/**
* Вставка контента в новый документ
*/
$fileMetadata = array(
'data' => $dd);
$filenew = new Google_Service_Drive_DriveFile();
$file = $service->files->update($new_file_id, $filenew, $fileMetadata);
it creates a document with paper size set to Letter. For my account the default is A4 and I want the newly created document to be in this format.
Does anyone know a way set the paper size of the uploaded document?
P.S.: I saw this thread, but may have appeared a method?
Google drive contains files in what is called a file resource it is basically a metadata list of information about the file. Some of these fields can be updated some can not. The fields that are write about state that on the page I linked above.
There is no information about paper size. That is probably because drive doesn't care about paper size that is something the local machine sets at the time it prints the file.
Answer: No you cant set paper size for a file on Google drive
You can do it with Apps Script. It's a bit hacky, but should work. You'll need to :-
create an Apps Script function that uses the Document service (see https://developers.google.com/apps-script/reference/document and https://developers.google.com/apps-script/reference/document/body#setPageHeight%28Number%29 to manipulate the page parameters for a given document ID
Publish that script https://developers.google.com/apps-script/guides/content so that it can be called as an endpoint.
After you import your file as you are currently doing, G Drive will return the ID of the newly created file into $file. Use this ID ($file->getId()) to call the endpoint from step 2.

Using Google Open ID Connect to Access User's Gmail Account

How can I run a google apps script API on multiple gmail accounts? I currently have a script that accesses a user's gmail when it is authorized by the two files shown below. I store the refresh token for the client once the code asks for authorization. However, how can I use Open ID to get authorization for various users' accounts? Do I need to store a client id in addition to a refresh token to gain authorization for a user's gmail account? Thanks for taking the time to help in advance!
read_email.php
<?php
require_once '../google-api-php-client/src/Google/autoload.php';
function getEmails (){
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setScopes(array(
'https://mail.google.com/'
));
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
if($client->isAccessTokenExpired()){
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/email_database/php/oauth2callback.php');
}
// Get the API client and construct the service object.
$service = new Google_Service_Script($client);
$scriptId = '**********************';
// Create an execution request object.
$request = new Google_Service_Script_ExecutionRequest();
set_time_limit(0);
$request->setFunction('test');
$response = $service->scripts->run($scriptId, $request);
$response = $response->getResponse();
$response = $response['result'];
return ($response);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/email_database/php/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
?>
oauth2callback.php
<?php
require_once '../google-api-php-client/src/Google/autoload.php';
include 'functions.php';
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/email_database/dashboard/php/oauth2callback.php');
$client->setScopes(array(
'https://mail.google.com/'
));
$client->setAccessType('offline');
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/email_database/dashboard/php/read_email.php';
$username = $_SESSION['username'];
$user = getUser($username);
if($user['refresh_token'] == null){
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
//echo $_GET['code'];
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$google_token = json_decode($_SESSION['access_token']);
print_r($google_token);
$refresh_token = $google_token->refresh_token;
addRefreshToken($username,$refresh_token);
//header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
else {
$refresh_token = $user['refresh_token'];
$client->refreshToken($refresh_token);
$_SESSION['access_token']= $client->getAccessToken();
// header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>

Google API for analytics invalid grant but works sometimes

I've been on this problem now for 2 days and have tried looking under google code and stackoverflow but still can come up with an answer.
My problem is when I try my google analytics api I get "Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'"
But the weird part is that some times it will work. rarely but if I refresh and keep trying it, it will output.
The only thing I could find is that it could be the refresh token limit has been exceeded
Attached is my code if someone could help me out or point me to the right direction.
Thanks!
<?php
session_start();
require_once 'Google_Client.php';
require_once 'Google_AnalyticsService.php';
require_once 'config.php';
$keyFile = 'key.p12';
$client = new Google_Client();
$client->setApplicationName("test");
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$client->setAssertionCredentials(new Google_AssertionCredentials(
GOOGLE_SERVICE_ACCOUNT,
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents($keyFile))
);
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setAccessType('offline');
$client->setUseObjects(true);
$service = new Google_AnalyticsService($client);
try {
$results = $service->data_ga->get(
'ga:44444444',
date('Y-m-d', strtotime('-30 days '.date('Y-m-d', strtotime('-1 day '.date('Y-m- d'))))),
date('Y-m-d', strtotime('-1 day '.date('Y-m-d'))),
'ga:visits,ga:newVisits',
/*array(
'dimensions' => 'ga:source,ga:keyword',
'sort' => '-ga:visits,ga:keyword',
'filters' => 'ga:medium==organic',
'max-results' => '25'
)*/
array('dimensions' => 'ga:date')
);
} catch (Google_ServiceException $e) {
// echo $e->getMessage();
}
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
$dateParsePattern = '/"Date.parse\(\\\"((\d{4})-(\d{2})-(\d{2})) UTC\\\"\)"/';
$dateParseReplacement = 'Date.parse("$1 UTC")';
$allVisitsItems = array();
$newVisitorsItems = array();
if ($results && count($results->getRows()) > 0) {
foreach ($results->getRows() as $row) {
$date = 'Date.parse("'.date("Y-m-d", strtotime($row[0])).' UTC")';
$allVisitsItems[] = array($date, intval(htmlspecialchars($row[1], ENT_NOQUOTES)));
$newVisitorsItems[] = array($date, intval(htmlspecialchars($row[2], ENT_NOQUOTES)));
}
}
header('Content-Type: application/json');
?>
<?php echo preg_replace($dateParsePattern, $dateParseReplacement, json_encode($allVisitsItems)) ?>
Edit - It's not the NTP, when I echoed date('l jS \of F Y h:i:s A'); it matched up.
The following works to generate output - make sure you are running PHP 5.3 or higher.
<?php
require_once './src/Google_Client.php';
require_once './src/contrib/Google_AnalyticsService.php';
$path_to_keyfile = '.p12';
$service_account_email = '7#developer.gserviceaccount.com';
$client_id = '7.apps.googleusercontent.com';
$analytics_profile_id = 'ga:IN URL OF ANALYTICS';
$client = new Google_Client();
$client->setApplicationName("API TEST");
$client->setAssertionCredentials(
new Google_AssertionCredentials(
$service_account_email,
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents($path_to_keyfile)
)
);
$client->setClientId($client_id);
$client->setAccessType('offline_access');
$service = new Google_AnalyticsService($client);
$from = date('Y-m-d', strtotime('-30 days '.date('Y-m-d', strtotime('-1 day '.date('Y-m-d'))))); // 30 days
$to = date('Y-m-d', strtotime('-1 day '.date('Y-m-d'))); // yesterday
$dateParsePattern = '/"Date.parse\(\\\"((\d{4})-(\d{2})-(\d{2})) UTC\\\"\)"/';
$dateParseReplacement = 'Date.parse("$1 UTC")';
$allVisitsItems = array();
$newVisitorsItems = array();
try {
$data = $service->data_ga->get(
//
$analytics_profile_id,
$from,
$to,
'ga:visits,ga:newVisits',
array('dimensions' => 'ga:date')
);
if($data && $data['totalResults'] > 0) {
foreach($data['rows'] as $row) {
$date = 'Date.parse("'.date("Y-m-d", strtotime($row[0])).' UTC")';
$allVisitsItems[] = array($date, intval(htmlspecialchars($row[1], ENT_NOQUOTES)));
$newVisitorsItems[] = array($date, intval(htmlspecialchars($row[2], ENT_NOQUOTES)));
}
}
header('Content-Type: application/json');
?>
<?php echo preg_replace($dateParsePattern, $dateParseReplacement, json_encode($allVisitsItems)) ?>
<?php echo preg_replace($dateParsePattern, $dateParseReplacement, json_encode($newVisitorsItems)) ?>
<?php
} catch(Exception $e) {
echo 'There was an error : - ' . $e->getMessage();
}

google drive api dont know where it saves the file but when fetched it shows the saved but not the others

I used the google drive api to insert a text file...
Build Service
function buildService() {
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$SERVICE_ACCOUNT_EMAIL = 'xxxx#developer.gserviceaccount.com';
if($_SERVER['HTTP_HOST'] == 'localhost')
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'xxxxxxxx-privatekey.p12';
else
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = $_SERVER['DOCUMENT_ROOT'].'/xxxxx-privatekey.p12';
$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
$SERVICE_ACCOUNT_EMAIL,
array($DRIVE_SCOPE),
$key);
$client = new Google_Client();
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);
return new Google_DriveService($client);
}
Retrieve Files
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
Creating an instance and inserting
$service = buildService();
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = "contents";
$createdFile = $service->files->insert($file, array('data' => $data,'mimeType' =>'text/plain',));
print_r($createdFile);
echo '<br>------<br>';
Retrieving files
$ret = retrieveAllFiles($service);
echo '<pre>';
print_r($ret);
As in the fist call i insert a file...
in the second call i am trying to list all files...
when i try to list i could see only the inserted file but not the others which i could see in my drive by logging into docs.google.com. i am already having 6 files online
as per the documentation i created service account with the private keys and the service account email.
so where will that file be store and how do i list all other files in the root of google drive?
You are inserting files in the application-owned account represented by the service account.
If you want to manage (insert/list/...) files in another domain user's Drive account, you'll have to perform Domain-wide delegation: https://developers.google.com/drive/delegation