ZendFramework Google Contacts - php

I am using the following
http://www.ibm.com/developerworks/opensource/library/x-phpgooglecontact/index.html
to authenticate to Google and retrieve a users contacts. However I would prefer this system to use Google's oAuth integration.
What is the easiest way to do this or a simple example?
Essentially I want to modify this
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cp');
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
// perform query and get feed of all results
$query = new Zend_Gdata_Query(
'http://www.google.com/m8/feeds/contacts/default/full');
$query->maxResults = 1000;
$query->setParam('sortorder', 'descending');
$feed = $gdata->getFeed($query);
Such that it now uses oauth2 such that a user never has to give their email or password.

I found the following tutorial which resolved my problem.
http://leandroardissone.com/post/1330339821/google-contacts-php

Related

Webmaster Tools API

I've been having an issue with the Webmaster Tools API. I am trying to make the script have no user interaction and get information that I need. I've got Google Analytics working with Service Accounts but this isn't supported for the Webmaster Tools.
I'm currently trying to use a API Server Key however when I try to query the API it returns :
Code 403:
"User does not have sufficient permission for site"
However I am owner of the site I am trying to query. I have checked www.google.com/webmasters/tools/home and the site is verified.
So I'm asking is my code correct?
$KEY_FILE_LOCATION = "privateKey.json";
$viewID = "view id"; //Website View ID - including ga:
$scopes = array(
"https://www.googleapis.com/auth/analytics.readonly",
"https://www.googleapis.com/auth/webmasters.readonly"
);
$emailAddr = 'Email'; //Got off google developer console
$apiKey = 'API Server Key'; //Got off google developer console
//Create Google Client
$client = new Google_Client();
$client->setApplicationName('Logging in Attempt');
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes($scopes);
$client->setAccessType('offline');
$client->setDeveloperKey($apiKey);
//Creating Webmaster Service
$webmastersService = new Google_Service_Webmasters($client);
//Creating Request
$request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$request->setStartDate('2016-07-01');
$request->setEndDate('2016-07-10');
$request->setDimensions( array('query') );
$requestWebsite = 'Website'; //The website I have access to
//Querying Webmaster
$qsearch = $webmastersService->searchanalytics->query($requestWebsite, $request);
$rows = $qsearch->getRows();
echo "<pre>",print_r($rows),"</pre>";
Found out where I went wrong. The code was fine, the problem was that on the search console. The website was verified however I needed to add the email address from the Service Account into the website and gave it restricted access. Once that was in there the script worked.

Add contact for Impersonation Account

I'm using https://github.com/jamesiarmes/php-ews for Exchange Web Services and I'm having a problem adding a contact for an account.
When I login using my impersonation details:
$ews = new ExchangeWebServices($this->server_address, $this->server_username, $this->server_password);
I want to create a new contact, e.g.:
$request = new EWSType_CreateItemType();
$contact = new EWSType_ContactItemType();
$contact->Initials = $this->relation->initials;
$contact->GivenName = $this->relation->first_name;
$contact->MiddleName = $this->relation->insertion;
$contact->Surname = $this->relation->last_name;
$request->Items->Contact[] = $contact;
$result = $ews->CreateItem($request);
I want to add this contact to a account that exists in my impersonation account list.
I want to avoid directly login in as an exchange user like:
$ews = new ExchangeWebServices($this->server_address, 'some#outlook.account', 'somepassword');
Is this possible? How would I achieve such a thing? Thanks for reading!
I'd suggest you most away from james's php-ews, it's unmaintained and doesn't follow any of the PSR's. I'd suggest you take a look at my own fork, garethp/php-ews. Creating a contact would be rather similar, but impersonation is made easy by my fork (example here) and if that's not working for you, you can always drop me a Github issue and I'll try to help out

Google contacts via API

I want to fetch Google contacts for users and create logs of new and updated contacts via a PHP script. Can someone please guide me on how to proceed?
I have implemented the same kind of service for calendar events using 0Auth2.0, but for contacts I didn't find any API in the Google PHP client libraries.
The problem is in the example, it seems that the json encoder does the mess.
One of the developers wrote how to achive email address on this link
http://pastebin.com/kAYT5Jng
You can see part of the discusion right here
Have you tried this project
https://github.com/rapidwebltd/php-google-contacts-v3-api
Fetching all the contacts :
$contacts = rapidweb\googlecontacts\factories\ContactFactory::getAll();
var_dump($contacts);
create new contact
$name = "Frodo Baggins";
$phoneNumber = "06439111222";
$emailAddress = "frodo#example.com";
$note = "Note for example";
$newContact = rapidweb\googlecontacts\factories\ContactFactory::create($name, $phoneNumber, $emailAddress, $note);

Google Calendar API using Zend Library

I'm following the tutorial on http://www.ibm.com/developerworks/library/x-googleclndr/ for working with the Google Calendar API, but I'm running into some strange problems. My PHP code looks like this
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Http_Client');
//create authenticated HTTP client for Calendar service
$gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$user = "daniel.lieberman610#gmail.com";
$pass = "*******";
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
$gcal = new Zend_Gdata_Calendar($client);
//generate query to get event list
$query = $gcal->newEventQuery();
$query->setUser($user);
$query->setVisibility('private');
$query->setProjection('basic');
//get and parse calendar feed
//print output
try {
$feed = $gcal->getCalendarEventFeed($query);
echo "<!--comment-->\n";
} catch (Exception $e) {
echo "<!--different comment-->\n";
}
Some kind of exception is occurring inside the try block, but not the Zend_Gdata_App_Exception because the error message is not being displayed. I can't figure out what I'm doing wrong.
Also, I am learning PHP as I go along here. Thanks a lot for your help.
Move the try code to the beginning of the file. Is there an error shown? Here you will find a working example which I'm using in my Joomla extension https://github.com/Digital-Peak/GCalendar/blob/master/com_gcalendar/admin/libraries/GCalendar/GCalendarZendHelper.php
By the way I suggest to move to the new google calendar API v3 because they shut down version 2 (which uses Zend) probably next year. Here is the link to the libs https://developers.google.com/google-apps/calendar/downloads

Specifying Parameters in Zend_GData when using an Oauth Token?

So, I figured out how to get an access token from Google using the Zend_Oauth library in 1.10. Now lets say I want to get my contacts...
$config = array(
'consumerKey' => 'zzz',
'signatureMethod' => 'HMAC-SHA1',
'consumerSecret' => 'xxx'
);
$token = unserialize($_SESSION['GOOGLE_ACCESS_TOKEN']);
$client = $token->getHttpClient($config);
$client->setMethod(Zend_Http_Client::GET);
// $client->setParameterGet('max-results', '10000');
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full');
// $query->MaxResults=100;
$feed = $gdata->getFeed($query);
$feed is a lovely object with 25 contacts. But if I want to get more in a single pull, there doesn't seem to be a way of specifying max results that works.
If I uncomment client->setParameterGet it's ignored. It works if I specify $client->setUri and use $rawdata = client->request() to get the response, but then other issues crop up with handling the feed data that comes back... like getting it into GData for easy handling.
I've tried $feed = $gdata->importString($rawdata->getBody()) but while $rawdata->getBody() returns what seems to be valid XML, $feed->totalResults throws an error, while it wouldn't if I used $gdata->getFeed($query).
If I uncomment $query->MaxResults=100; use $gdata->getFeed($query) Google returns a 401 with "Unknown authorization header".
So is it possibly to specify parameters while using Zend_GData with an Oauth token? Or am I going to have to build my own requests, then use Zend_Feed (or some other XML/Feed dissector) for parsing?
I totally cannot get the whole list of contacts only 25... parameters do not seem to work using Gdata and query like this:
$http = $token->getHttpClient($oauthOptions);
$gdata = new Zend_Gdata($http, 'MY APP');
$gdata->setMajorProtocolVersion(3);
$gdata->getHttpClient()->setRequestScheme(Zend_Oauth::REQUEST_SCHEME_QUERYSTRING);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full?max-results=10');
$query->setMaxResults(10);
$query->maxResults = 10;
$feed = $gdata->getFeed($query);
so i;m really into finding answers here as well. If either of you gets anything working. please post :-)
thanks
It's a bit tricky mixing a process meant to work with AuthSub with OAuth. I did some digging. So far I can get it to download all my contacts like this...
$client = $token->getHttpClient($config);
$client->setMethod(Zend_Http_Client::GET);
$client->setUri('http://www.google.com/m8/feeds/contacts/default/full/');
$client->setParameterGet('max-results', '10000');
$client->setParameterGet('v','3');
$bfeed = $client->request();
Looks like the primary difference between us is I specify the Feed URL in the $client->setUri('http://www.google.com/m8/feeds/contacts/default/full/'); and set my version differently. But I can get the body() property of $bfeed and it gives me 245k of XML to dissect.
My problem is that when I'm pulling down a single contact's feed via this method, I was getting an error.
I, like you, am trying to figure this out, so please reply with anything that works for you.

Categories