我想使用API PHP在Google+上发布图片。 我有一个错误:
Fatal error: Uncaught exception 'Google_ServiceException' with message
'Error calling POST [...] (401) Invalid Credentials'
这是我的代码。问题是什么?谢谢你的帮助。
require_once './src/Google_Client.php';
require_once './src/contrib/Google_PlusService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('*****');
$client->setClientId('*******.apps.googleusercontent.com');
$client->setClientSecret('*********');
$client->setRedirectUri('******/callback.php');
$client->setDeveloperKey('*********');
$client->setScopes(array('https://www.googleapis.com/auth/plus.login'));
$client->setUseObjects(true);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (!$client->getAccessToken()) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
} else {
$plus = new Google_PlusService($client);
$moment_body = new Google_Moment();
$moment_body->setType("http://schemas.google.com/AddActivity");
$item_scope = new Google_ItemScope();
$item_scope->setId("target-id-1");
$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("./image.png");
$moment_body->setTarget($item_scope);
$momentResult = $plus->moments->insert('me', 'vault', $moment_body);
}
答案 0 :(得分:1)
不要忘记为请求设置request_visible_actions参数:
$requestVisibleActions = array('http://schemas.google.com/AddActivity');