【问题标题】:Insert Moment in Google+ using Google PHP Client使用 Google PHP 客户端在 Google+ 中插入时刻
【发布时间】:2013-07-30 15:19:02
【问题描述】:

我正在尝试在 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>';
}

【问题讨论】:

    标签: php google-api google-plus


    【解决方案1】:

    您需要将 requestvisibleactions 权限添加到您的范围。最简单的方法是从传统的 OAuth 2.0 流程切换到 new Google+ Sign-In flow - Google+ 团队提供 a PHP sample for Google+ Sign-In。如果您想继续使用旧的 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);
    

    要查看您编写的应用活动,请访问app activity log

    【讨论】:

    • 我试过添加这一行,但没有解决任何问题 $client->setRequestVisibleActions(array('schemas.google.com/AddActivity')); (我使用的是 PHP Client v0.6.5) PS:cmets 表单隐藏了 url 的 http:// 部分,但它存在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 2020-09-23
    • 2016-12-03
    • 1970-01-01
    相关资源
    最近更新 更多