Gmail API - Update draft message keeping attachments - php

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?

Related

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);
}

Parse PHP SDK doesn't return query result

I was able to install the Parse PHP SDK (version 1.1.2) to my project and got through the quick start guide in successfully creating a TestObject. However, when I tried to query the objects, it returns nothing. I'm stuck and don't know where to go from here.
use Parse\ParseQuery;
$query = new ParseQuery('TestObject');
$results = $query->find();
foreach($results as $result){
echo $result->getObjectId() ." - " $result->get("foo") . "\n";
}
You could try it this way:
$myObj= new ParseObject("TestObject");
$myObj->set("objIdd", 1234);
$myObj->set("objName", "My Name");
try {
$myObj->save();
echo 'New object created with objectId: ' . $myObj->getObjectId();
} catch (ParseException $ex) {
// Execute any logic that should take place if the save fails.
// error is a ParseException object with an error code and message.
echo 'Failed to create new object, with error message: ' + $ex->getMessage();
}
//objectId: "XYZABC", objIdd: 1234, objName: "My Name"
$query = new ParseQuery("TestObject");
try {
$myObj = $query->get("XYZABC");
// The object was retrieved successfully.
} catch (ParseException $ex) {
// The object was not retrieved successfully.
// error is a ParseException with an error code and message.
}
Take a look at this guide.

Google Drive PHP API V2 No results when listing

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

Problem using publishUserAction method to post to profile

I am using gigya php sdk, It works well I am able to post to user's wall or profile using socialise.setStatus method but I am have problems when I try to use the publishuseraction method. I get an error Invalid request signature.
$method = "socialize.publishUserAction";
$request = new GSRequest($apiKey,$secretKey,$method);
$userAction = new GSDictionary("{\"title\":\"This is my title\", \"userMessage\":\"This is a user message\", \"description\":\"This is a description\", \"linkBack\":\"http://google.com\", \"mediaItems\":[{\"src\":\"http://www.f2h.co.il/logo.jpg\", \"href\":\"http://www.f2h.co.il\",\"type\":\"image\"}]}");
$request->setParam("userAction", $userAction);
$request->setParam("uid", $userUID);
$response = $request->send();
if($response->getErrorCode()==0){
echo "Success";
} else {
echo ("Error: " . $response->getErrorMessage());
}
UPDATED AFTER USING $response->getLog()
apiMethod=socialize.publishUserAction
apiKey=correct_api_key
params={"userAction":{},uid":"MY_UID","format":"json","httpStatusCodes":"false"}
URL=http://socialize.gigya.com/socialize.publishUserAction postData=uid=urlencoded(MY_UID)&format=json&httpStatusCodes=false&apiKey=correct_api_keyƗtamp=1296229876&nonce=1.29622987636E%2B12&sig=HEdzy%2BzxetV8QvTDjdsdMWh0%2Fz8%3D
server= web504
I used both put method
$userAction = new GSDictionary();
$userAction->put("title", "This is my title");
$userAction->put("userMessage", "This is my user message");
$userAction->put("description", "This is my description");
$userAction->put("linkBack", "http://google.com");
$mediaItems = array();
$mediaItems[0] = new GSDictionary("{\"src\":\"http://www.f2h.co.il/logo.jpg\", \"href\":\"http://www.f2h.co.il\",\"type\":\"image\"}");
& JSON method
$userAction = new GSDictionary("{\"title\":\"This is my title\", \"userMessage\":\"This is a user message\", \"description\":\"This is a description\",
\"linkBack\":\"http://google.com\", \"mediaItems\":[ {\"src\":\"http://www.f2h.co.il/logo.jpg\", \"href\":\"http://www.f2h.co.il\",\"type\":\"image\"}]}");
I get the same error. And User action is empty using both methods. I appreciate any help.
Thanks.
Try calling getLog() for a bit more information about the error:
if($response->getErrorCode()==0){
echo "Success";
} else {
echo ("Error: " . $response->getErrorMessage());
print "<pre>";
print_r($response->getLog());
print "</pre>";
}
Also, are you able to run getUserInfo w/ the SDK? This is a simple test that also requires a signature. Use the example on the PHP SDK page (about 1/2 way down).

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