在谷歌+;使用PHP的API.insert
我想创建一个小机器人,它可以在特定条件下将G+时刻推送到G+页面。想用PHP试试吗。所以我在谷歌控制台上注册了web应用程序,打开了Google+API,在我的Apache服务器上安装了一个小PHP脚本,结果是401未被优化。我正在使用google api php客户端 我在网上搜索了答案,但什么也没找到。每个人都有自己的幸福结局,都很成功,但我尝试了所有的一切——只有401计划来到我身边 我的剧本:在谷歌+;使用PHP的API.insert,php,google-plus,google-api-php-client,Php,Google Plus,Google Api Php Client,我想创建一个小机器人,它可以在特定条件下将G+时刻推送到G+页面。想用PHP试试吗。所以我在谷歌控制台上注册了web应用程序,打开了Google+API,在我的Apache服务器上安装了一个小PHP脚本,结果是401未被优化。我正在使用google api php客户端 我在网上搜索了答案,但什么也没找到。每个人都有自己的幸福结局,都很成功,但我尝试了所有的一切——只有401计划来到我身边 我的剧本: <? header("Content-Type: text/html; charset=
<?
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;
}
您可能缺少一些东西。也许您的示例假设您正在为其中一些设置的值输入实际值。但是,您需要设置ApplicationName和DeveloperKey。如果没有确定的开发人员密钥设置,许多api调用将返回not authorized
$client->setApplicationName($plApp->authenticationData->applicationName);
$client->setDeveloperKey($authData->apiKey);
我想你实际上是在下面设置这些。返回url必须与在google开发者网站上为特定应用程序设置的应用程序url匹配
$client->setClientId($authData->clientId);
$client->setClientSecret($authData->clientSecret);
$client->setRedirectUri($returnUrl);
是否在开发者控制台()中为该项目启用了Google+API?是的,我已经启用了written@RonhulMaggot你知道怎么回事吗?不,我不得不买剧本来帮我完成这项工作。