【发布时间】:2014-04-03 17:38:24
【问题描述】:
我想创建一个小机器人,它可以在特定条件下将 G+ 时刻推送到 G+ 页面。想用 PHP 试试。所以我有:在 Google 控制台中注册了 Web 应用程序,打开了 Google+ API,我的 Apache 服务器上的小型 PHP 脚本和 401 Unathorized 结果。我正在使用来自https://github.com/google/google-api-php-client的google-api-php-client
我已经通过互联网搜索了答案,但没有找到任何答案。每个地方的每个人都有自己的快乐结局并且都工作,但我已经尝试了所有这些 - 只有 401 来找我。
我的脚本:
<?
header("Content-Type: text/html; charset=utf-8");
session_start();
ini_set("display_errors", 1);
require_once 'Google/Client.php';
require_once 'Google/Service/Plus.php';
$client = new Google_Client();
$client->setClientId("MY_CLIENT_ID");
$client->setClientSecret("MY_CLIENT_SECRET");
$client->setRedirectUri("REDIRECT_URL");
$client->setAccessType('offline');
$client->addScope("https://www.googleapis.com/auth/plus.login");
$client->addScope("https://www.googleapis.com/auth/plus.me");
$client->addScope("https://www.googleapis.com/auth/userinfo.profile");
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
$requestVisibleActions = array('http://schemas.google.com/AddActivity');
$client->setRequestVisibleActions($requestVisibleActions);
if (!isset($_GET['code'])) {
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
$moment_body = new Google_Service_Plus_Moment();
$plus = new Google_Service_Plus($client);
$moment_body->setType("http://schemas.google.com/AddActivity");
$item_scope = new Google_Service_Plus_ItemScope();
$item_scope->setId("target-id-214wdefsadf1");
$item_scope->setType("http://schemas.google.com/AddActivity");
$item_scope->setName("The Google+ Platform");
$item_scope->setDescription("A page that describes just how awesome Google+ is!");
$item_scope->setImage("https://developers.google.com/+/plugins/snippet/examples/thing.png");
$moment_body->setTarget($item_scope);
$momentResult = $plus->moments->insert('me', 'vault', $moment_body);
} else {
$authUrl = $client->createAuthUrl();
header("Location: ".$authUrl);
exit;
}
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header("Location: REDIRECT_URL");
exit;
}
因此脚本成功地请求所有需要的访问权限,相应地在客户端注册的范围,获取令牌,将其写入会话,但是当它尝试插入新的时刻时,它得到 致命错误:未捕获的异常 'Google_Service_Exception' 和消息'错误调用 POST https://www.googleapis.com/plus/v1/people/me/moments/vault: (401) Unauthorized' in /var/www/rico/Google/Http/REST.php:79
怎么了?我已经尝试了一些在这里放置的解决方案,在 stackoveflow 上,但它们没有帮助。我还检查了我的 authURL - 看起来不错,有正确的 request_visible_actions 和其他...我不知道出了什么问题...
【问题讨论】:
-
是否在开发者控制台 (developers.google.com/console) 中为该项目启用了 Google+ API?
-
是的,我已经写过了
-
@RonhulMaggot 你有没有弄清楚出了什么问题?
-
不,我必须购买可以为我完成这项工作的脚本。
标签: php google-plus google-api-php-client