0

我正在尝试在 Google+ 个人资料中插入一些活动应用程序,如本文档页面所示: https ://developers.google.com/+/api/latest/moments/insert

我成功获得了所需的访问令牌,但似乎 moment->insert 方法没有做任何事情。

如果成功,我希望在此页面上看到一些东西,一旦访问,但什么都没有发生 https://plus.google.com/u/0/apps/activities

这是我的代码

<?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>';
}
4

1 回答 1

1

您需要将 requestvisibleactions 权限添加到您的范围。最简单的方法是从传统的 OAuth 2.0 流程切换到新的 Google+ 登录流程- Google+ 团队为 Google+ 登录提供了一个 PHP 示例。如果您想继续使用旧的 OAuth 流程,您需要将 request_visible_actions=[应用活动类型] 附加到您的授权 URL。

相关问题:

在您的代码中,您真的很接近,以下似乎对我有用:

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

要检查您编写的应用活动,请访问应用活动日志

于 2013-07-30T18:24:57.767 回答