Google Drive PHP API V2 No results when listing - php

I'm creating a module for joomla that lists the the contents of a given folder. This module works as a service, so no OAuth should be required.
I'm stuck since every request I make has the same answer, no items. So I would like to know if I'm creating the client/service in a wrong way or I may have registered the application in a wrong way.
Here is the code:
// this class is where I have all the IDs from the google console
require_once(dirname(__FILE__) . '/gdrive/config.php');
require_once(dirname(__FILE__) . '/gdrive/gd-v2-php/apiClient.php');
require_once(dirname(__FILE__) . '/gdrive/gd-v2-php/contrib/apiDriveService.php');
$client = new apiClient();
$key = file_get_contents(Config::KEY_FILE);
$client->setClientId(Config::CLIENT_ID);
$client->setUseObjects(true);
$client->setAssertionCredentials(new apiAssertionCredentials(
Config::SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive'), $key)
);
$service = new apiDriveService($client);
try {
$parameters = array();
$parameters['maxResults'] = $maxResults;
$files = $service->files->listFiles($parameters);
$items = $files->getItems();
var_dump($items);
$pageToken = $files->getNextPageToken();
} catch (apiServiceException $e) {
print "Error code :" . $e->getCode() . "\n";
// Error message is formatted as "Error calling <REQUEST METHOD> <REQUEST URL>: (<CODE>) <MESSAGE OR REASON>".
print "Error message: " . $e->getMessage() . "\n";
} catch (apiException $e) {
// Other error.
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
} catch (Exception $e) {
print "Cannot retrieve the files";
print($e->getMessage());
}
Sorry to ask such a noob question but this is driving me crazy
Thanks
P.S. just for the record, I followed this google-api-php-client/wiki/OAuth2#Service_Accounts

After having a second look to the documentation I found that Google Drive does not support service accounts. I don't know if this is going to change but, with the requirements I have (I need to show in the web is what an end user uploads to a folder (which is public) using the desktop app) I think I cannot use google drive as a valid solution.
I found a solution but I don't like to much since I should have to refresh the token forever Google Drive SDK Service Account Error

Related

Gmail API - Update draft message keeping attachments

Scenario:
I am using Gmail API via PHP.
Purpose:
I want to edit the HTML part of the message.
Precisely, I want to add a tracking image in the message, keeping the attached files.
What am I doing?:
This:
//UPDATE
$opt_param = array();
$d = new Google_Service_Gmail_Draft();
$d->setMessage($msg);
try {
$plom = $service->users_drafts->update('me', $draftito, $d, $opt_param);
echo 'Draft with ID: ' . $draftito . ' updated successfully.<hr><hr>';
var_dump($plom);
} catch (Exception $e) {
echo 'An error occurred: ' . $e->getMessage();
}
//SEND
$de = new Google_Service_Gmail_Draft();
$de->setId($draftito);
$plom=$service->users_drafts->send('me', $de);
var_dump($plom);
Probably the key point here is $msg.
I set it up with setRaw() message:
$mime = ...
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
But I do not know how to handle the attached files here.
I know I am getting things mixed up, but the online docs for PHP are really obscure.
Is there any way to just edit the HTML version of the message without touching all the multipart Message?

How to dynamically query couch db

I am using php-on-couch library to retrieve data from couchdb
I want to retrieve basing on where condition. I want data according to the user who is signed in. My code below retrieves a view called accounts but i want to be retrieving without creating views on couchdb. Any help is highly appreciated
try {
$doc = $client->asArray()->getView('accounts', 'accounts');
} catch (Exception $e) {
if ($e->code() == 404) {
echo "View not found\n";
} else {
echo "Error: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
}
exit(1);
}
print_r($doc);
is there a better way to dynamically create a view on retrieving just like mysql does?
using php-on-couch you can do this
try {
$opts = array("include_docs" => true, "key" => "your user name");
$doc = $client->setQueryParameters($opts)->getView("accounts", "accounts");
} catch (Exception $e) {
if ($e->code() == 404) {
echo "Account not found\n";
} else {
echo "Error: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
}
exit(1);
}
print_r($doc);
Make sure you emit the username as the key in the accounts view. Couch db will search in all accounts in the view and give you the doc that contains your username.
emit(doc.username, doc);
Hope it helps the rest of you using couchdb in php

PHP Gmail API - Send draft

How may I send a draft with GMAIL API and OAuth2.0 via PHP?
In the official docs there is no reference on how to achieve this with PHP.
Based on the Java example, I tried:
$drafts = array();
try {
$draftsResponse = $service->users_drafts->listUsersDrafts('me');
if ($draftsResponse->getDrafts()) {
$drafts = array_merge($drafts, $draftsResponse->getDrafts());
}
}
catch (Exception $e) {
echo 'An error occurred: ' . $e->getMessage();
}
var_dump($drafts);
foreach ($drafts as $draft) {
echo 'Draft with ID: ' . $draft->getId() . '<br/>';
$abc = $service->users_drafts->send('me',$draft->getId());
var_dump($abc);
}
But of course I am doing something wrong, because it is not working. The first var_dump() returns all drafts. But nothing else happens after that.
Can you please help me?
You have to create a new Google_Service_Gmail_Draft instance and use that, not just supply the id:
foreach ($drafts as $draft) {
$d = new Google_Service_Gmail_Draft();
$d->setId($draft->getId());
$service->users_drafts->send('me', $d);
}

Inserting Events to google calendar from a script

Anyone know the proper way to authenticate and publish directly to a calendar without relying on a currently logged in user? Several weeks ago I built a calendar that used the standard Oauth 2.0 protocol but this relied sessions stored by a user's we browser. I have one calendar that I want to pass events to from an application I am writing with a basic PHP framework. I'm more concerned with what are the best practices that others are using. Your answer could be simply, don't do it. Thanks alot.
Use OAuth 2 and the Authorization Code flow (web server flow), with offline enabled. Store the refresh tokens (which last indefinitely until the user has revoked), and you'll be able to upload events to Google Calendar even when the user isn't currently logged in.
More info:
https://developers.google.com/accounts/docs/OAuth2WebServer#offline
try Zend_Gdata_Calendar with this library you are able to insert or get events from any user(with the right username and password obviously) from google calendar and integrate with your own calendar or display it..here a short example:
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient('gmail#user.com', 'gmailpassword', $service);
$service = new Zend_Gdata_Calendar($client);
$query = $service->newEventQuery();
$query->setUser('default');
$query->setVisibility('private');
try {
$eventFeed = $service->getCalendarEventFeed($query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
echo "<ul>";
foreach ($eventFeed as $event) {
echo "<li>" . $event->title . " (Event ID: " . $event->id . ")</li>";
}
echo "</ul>";
$eventURL = "http://www.google.com/calendar/feeds/default/private/full/Dir0FthEpUbl1cGma1lCalendAr";
try {
$event = $service->getCalendarEventEntry($eventURL);
echo 'Evento: ' . $event->getTitle() .'<br>';
echo 'detalles: ' . $event->getContent().'<br>';
foreach ($event->getWhen() as $dato)
{
echo 'inicia: ' . substr($dato->startTime, 0,-19) . ' a las: ' . substr($dato->startTime, 11,-10) .'<br>';
echo 'termina: ' .substr($dato->endTime,0,-19) . ' a las: ' . substr($dato->endTime,11,-10) .'<br>';
}
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
with this you can add, update, edit or delete events from calendar form any user with mail and password...

Trying to manipulate MS Word bookmarks using PHP and COM

I'm trying to manipulate some fields in a supplied Word 2003 document using the document's own bookmarks using PHP and COM but am getting an error which ever method that I use. If I try to call the Bookmarks directly to substitute the text I get an error: The range cannot be deleted.
function testBkMrkDetails($word, $bookmarkName, $subsTxt) {
try {
$BkMark = $word->ActiveDocument->Bookmarks($bookmarkName);
$range = $BkMark->Range;
if (!$range) {
echo "Range not created ";
}
$range->Text = $subsTxt;
} catch (Exception $e) {
echo "bookmark failed: " . $e->getMessage() . "\n";
}
}
I've searched on Google using the error message and loking for PHP and COM tutorials which appeared to suggest that I look at FormFields so I wrote the following:
function testFormFlds ($word, $bookmarkName, $subsTxt) {
try {
$formField = $word->Selection->FormFields($bookmarkName);
if (!$formField) {
die("Form failed : " . $bookmarkName . " not found\n");
}
$formField->Result($subsTxt);
} catch (Exception $e){
echo "FormField failed: " . $e->getMessage();
}
}
However this keeps telling me that the requested member of the collection does not exists so I'm assuming that I have not called the feild name correctly (it was taen from the document). I would be grateful for some pointers towards solving this and learning more about the underlying technology. Thanks, Iain

Categories