Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API PHP Client - Contacts Service

I have, after hours of deep, deep pain, finally got a bit closer to the configuration and usage of Google's API PHP Client, using this tutorial (based on Analytics though).

So, now I finally auth my self, in a way that seems legit and official. My natural thought was that there would exist an contrib/Google_ContactsService.php, but to my surprise, it is nowhere to find amogst the 32 other service classes.

I feel like back to scratch. Is there any way I can - legit and officially - fetch a specific users' contacts? (tons of tutorials out there, but all outdated and hacky).

EDIT: I notice there is a newer version of the library available here, but there is still not any "Contacts" service to be found in the .../Service/ folder

EDIT: My progress so far. The last lines fails with response from Google: 401. There was an error in your request. - I guess that is because of lack of permission (I didn't ask for Contacts permission). But how do I do this, without the "Google_ContactsService.php"? I am lost. See code:

<?php
session_start();

/**
 * Require the libaries
 */

    require_once 'assets/php/Google/Google_Client.php';
    require_once 'assets/php/Google/contrib/Google_AnalyticsService.php'; // from tutorial - I am supposed to get the Contacts Service, which is nowhere to find.

/**
 * Set up the Google_Client
 */

    $client = new Google_Client();
    $client->setAccessType('online'); // default: offline
    $client->setApplicationName($apiConfig['application_name']);
    $client->setClientId($apiConfig['oauth2_client_id']);
    $client->setClientSecret($apiConfig['oauth2_client_secret']);
    $client->setRedirectUri($apiConfig['oauth2_redirect_uri']);
    $client->setDeveloperKey($apiConfig['developer_key']); // API key

/**
 * $service implements the client interface, has to be set before auth call
 */

    $service = new Google_AnalyticsService($client);

/**
 * Log out
 */

    if (isset($_GET['logout'])) { // logout: destroy token
        unset($_SESSION['google_token']);
        exit('Logged out.');
    }

/**
 * Google auth code received
 *
 * Store access token
 */

    if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
        $client->authenticate();
        $_SESSION['google_token'] = $client->getAccessToken();
    }

/**
 * Set auth token
 */

    if (isset($_SESSION['token'])) { // extract token from session and configure client
        $token = $_SESSION['token'];
        $client->setAccessToken($token);
    }

/**
 * If no token, redirect and auth
 */

    if (!$client->getAccessToken()) { // auth call to google
        $authUrl = $client->createAuthUrl();
        header("Location: ".$authUrl);
        exit;
    }

/**
 * Get contacts
 */

    $access_token = json_decode($client->getAccessToken())->access_token;

    $url = 'https://www.google.com/m8/feeds/contacts/default/full?alt=json&v=3.0&oauth_token='.$access_token;

    $response =  file_get_contents($url);

    exit($response);

    echo 'Hello, world.';
like image 524
FooBar Avatar asked Mar 25 '14 10:03

FooBar


1 Answers

From here:

Unfortunately the contacts API is one of the older GData ones, while this library is for the newer APIs. You can use the OAuth part of the library to request the scope (https://www.googleapis.com/auth/contacts.readonly), and use the token to make the request, but you'll have to parse the data manually. Zend Framework does have the Zend_Gdata classes that might make reading the results a bit easier!

I found an example using the old library.

like image 177
Vinicius Pinto Avatar answered Sep 21 '22 08:09

Vinicius Pinto