简体   繁体   中英

Insert Moment in Google+ using Google PHP Client

I'm trying to insert some activity app in a Google+ profile as shown in this documentation page: https://developers.google.com/+/api/latest/moments/insert

I successfully obtain the access token needed, but seems the moments->insert method doesn't make anything.

If successful I would expect to see something on this page, once made the access, but nothing happen https://plus.google.com/u/0/apps/activities

That's my code

<?php
require_once '../google-api-php-client/Google_Client.php';
require_once '../google-api-php-client/contrib/Google_PlusService.php';

session_start();

$client = new Google_Client();
$client->setClientId('xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxxxxxxxxxxxxxx');
$client->setRedirectUri('http://www.myregisteredcallbackurl.com');
$client->setAccessType('offline');
$client->setScopes(array('https://www.googleapis.com/auth/plus.login'));
$client->setApprovalPrompt('force');

$plus = new Google_PlusService($client);

if (isset($_GET['logout'])) {
    unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
    echo '<a href="http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?logout">Logout</a><br><br>'.PHP_EOL.PHP_EOL;
    $client->setAccessToken($_SESSION['token']);

    $moment = new Google_Moment();
    $moment->setType('http://schemas.google.com/AddActivity');
    $itemScope = new Google_ItemScope();
    $itemScope->setUrl('https://developers.google.com/+/plugins/snippet/examples/thing');
    $moment->setTarget($itemScope);
    $plus->moments->insert('me', 'vault', $moment);
}

if ($client->getAccessToken()) {
    $_SESSION['token'] = $client->getAccessToken();
} else {
    $authUrl = $client->createAuthUrl();
    echo '<a href="' . $authUrl . '">Connect</a><br>';
}

You need to add the requestvisibleactions permissions to your scope. The easiest way to do this is to switch from the conventional OAuth 2.0 flow to the new Google+ Sign-In flow - the Google+ team provides a PHP sample for Google+ Sign-In . If you want to continue using the older OAuth flow, you need to append request_visible_actions=[the app activity types] to your authorization URL.

Related questions:

In your code, you are really close, the following seems to work for me:

$client = new Google_Client();
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('http://example.com/callback.php');
$client->setAccessType('offline');
$client->setScopes(array('https://www.googleapis.com/auth/plus.login'));
$client->setRequestVisibleActions(array('https://schemas.google.com/AddActivity'));

$plus = new Google_PlusService($client);

To check the app activities that you have written, visit the app activity log .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM