Skip to content

Instantly share code, notes, and snippets.

@silvolu
Last active June 30, 2016 10:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save silvolu/5054214 to your computer and use it in GitHub Desktop.
Save silvolu/5054214 to your computer and use it in GitHub Desktop.
Write moments with the trunk version of the Google APIs Client Library for PHP https://code.google.com/p/google-api-php-client/source/checkout
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();
$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
// set $requestVisibleActions to write moments
$requestVisibleActions = array(
'http://schemas.google.com/AddActivity',
'http://schemas.google.com/ReviewActivity');
$client->setRequestVisibleActions($requestVisibleActions);
$plus = new Google_PlusService($client);
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
// now write a moment
$moment_body = new Google_Moment();
$moment_body->setType("http://schemas.google.com/AddActivity");
$item_scope = new Google_ItemScope();
$item_scope->setUrl(
"https://developers.google.com/+/plugins/snippet/examples/thing");
$moment_body->setTarget($item_scope);
$momentResult = $plus->moments->insert('me', 'vault', $moment_body);
print_r($momentResult);
}
if ($client->getAccessToken()) {
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
$_SESSION['token'] = $client->getAccessToken();
} else {
// Sign in after user input
$authUrl = $client->createAuthUrl();
print '<a href="' . $authUrl . '">Connect Me!</a>';
}
@zubair1103
Copy link

Function does not exist
setRequestVisibleActions()

@saanya
Copy link

saanya commented Apr 19, 2013

Hi, I use your update code, but have exception
"error": {
"errors": [
{
"domain": "global",
"reason": "unauthorized",
"message": "Unauthorized"
}
],
"code": 401,
"message": "Unauthorized"
}
}

  1. Downloaded the api - google-api-php-client-0.6.2
  2. Copied your Google_Client.php from this link https://code.google.com/p/google-api-php-client/source/browse/trunk/src/Google_Client.php?spec=svn534&r=534
  3. and then downloaded your index.php from this link - https://gist.github.com/silvolu/5054214#file-index-php-L16

@arizalnurrohman
Copy link

i have try it, but mine is no action ,, jus show like this on my page ,,
do you have any solution ??

Array ( [kind] => plus#moment [type] => http://schemas.google.com/AddActivity [target] => Array ( [kind] => plus#itemScope [id] => target-id-1 [description] => A page that describes just how awesome Google+ is! [image] => https://developers.google.com/+/plugins/snippet/examples/thing.png [name] => The Google+ Platform ) [result] => Array ( [kind] => plus#itemScope ) [id] => Eg0xMzcyMjMyOTk4MDg2GPfC9fj_5PCUvAEyAhAUQgcYzvHT6N4XShCPf8D-ihBNxL0z7eK3kcJ_ )

or this

Array
(
[kind] => plus#moment
[type] => http://schemas.google.com/AddActivity
[target] => Array
(
[kind] => plus#itemScope
[id] => target-id-1
[description] => A page that describes just how awesome Google+ is!
[image] => https://developers.google.com/+/plugins/snippet/examples/thing.png
[name] => The Google+ Platform
)

[result] => Array
    (
        [kind] => plus#itemScope
    )

[id] => Eg0xMzcyMjMyOTk4MDg2GPfC9fj_5PCUvAEyAhAUQgcYzvHT6N4XShCPf8D-ihBNxL0z7eK3kcJ_

)

@kalsoomft
Copy link

could you tell me how i get $moment_body = new Google_Moment(); and $item_scope = new Google_ItemScope(); classes please.

@NourdineMazali
Copy link

Google_Moment is deprecated and are not supported in the v3 API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment