我想使用PHP API将评论或消息从我的网站发布到google plus wall。
这是我的代码。
include_once "templates/base.php";
session_start();
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
$client_id = '######';
$client_secret = '####';
$redirect_uri = 'http://www.example.com/idtoken.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setScopes(
array(
"https://www.googleapis.com/auth/plus.me"
)
);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
{
$client->setAccessToken($_SESSION['access_token']);
}
else
{
$authUrl = $client->createAuthUrl();
}
if ($client->getAccessToken()) {
$_SESSION['access_token'] = $client->getAccessToken();
$token_data = $client->verifyIdToken()->getAttributes();
}
$access_token=json_decode($_SESSION['access_token'])->access_token;
$url='https://www.googleapis.com/plusDomains/v1/people/102728965990039439412/activities';
$ch = curl_init( $url );
# Setup request to send json via POST.
curl_setopt( $ch, CURLOPT_POSTFIELDS, $text );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Authorization:OAuth'.$access_token));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
echo "<pre>";
print_r($result);
echo "</pre>";
exit;
以下是执行上述代码后得到的响应。
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
任何人都可以告诉我上面的代码有什么问题,或者我遗漏了什么?