我在Blogger中发布帖子时遇到问题,根据他们的documentation查询必须如下:
POST https://www.googleapis.com/blogger/v3/blogs/{blogId}/posts/
Authorization: {OAuth 2.0 token}
Content-Type: application/json
{
"kind": "blogger#post",
"blog": {
"id": "{blogId}"
},
"title": "A new post",
"content": "With <b>exciting</b> content..."
}
在我的情况下,我总是收到“凭据无效”,但在插入帖子时我仍然可以获取博客信息。
Returned response:
{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }
用于插入帖子的代码:
$postTitle = 'post test';
$postContent = 'just another test.';
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blogId.'/posts/';
$headerQuery = array();
$headerQuery[] = 'Authorization: '.$accessToken;
$headerQuery[] = 'Content-Type: application/json';
$headerQuery[] = ' { "kind": "blogger#post", "blog": {"id": "'.$blogId.'"}, "title": "'.$postTitle.'", "content": "'.$postContent.'" }';
//$headerQuery[] = 'Content-length: '.strlen($headerQuery[2]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerQuery);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
echo $data;
//echo curl_errno($ch);
$response = json_decode($data);
echo "url: " . $response->url."<br />";
echo "id: " . $response->id."<br />";
curl_close($ch);
答案 0 :(得分:4)
我在我的代码中发现了两个错误,第一个是对Google Plus进行身份验证,而我应该对Blogger进行身份验证。
<?php
$url = "https://accounts.google.com/o/oauth2/auth";
$params = array(
"response_type" => "code",
"client_id" => "XXXXXXXXXX.apps.googleusercontent.com",
"redirect_uri" => "http://localhost/phpBlogger/oauth2callback.php",
"scope" => "https://www.googleapis.com/auth/blogger"); // Request Blogger authentication
$request_to = $url.'?'.http_build_query($params);
header("Location: ".$request_to);
?>
if(isset($_GET['code'])) {
// Get an access token
$code = $_GET['code'];
$url = 'https://accounts.google.com/o/oauth2/token';
$apikey = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$blogId = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$params = array(
"code" => $code,
"client_id" => urlencode("XXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"),
"client_secret" => urlencode("XXXXXXXXXXXXXXXXXXXXXXXXX"),
"redirect_uri" => urlencode("http://localhost/phpBlogger/oauth2callback.php"),
"grant_type" => urlencode("authorization_code")
);
// HTTP query builder
foreach($params as $key=>$value) {
$fields .= $key.'='.$value.'&';
}
rtrim($fields, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
//echo curl_errno($ch);
$response = json_decode($data);
$accessToken = $response->access_token;
}
第二个错误是我如何构建JSON POST查询,我应该将Content-length添加到标题:
$postTitle = 'post test';
$postContent = 'just another test.';
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blogId.'/posts/';
$body = ' { "kind": "blogger#post", "blog": {"id": "'.$blogId.'"}, "title": "'.$postTitle.'", "content": "'.$postContent.'" }';
$headerQuery = array();
$headerQuery[] = 'Authorization: OAuth '.$accessToken;
$headerQuery[] = 'Content-Length: '.strlen($body);
$headerQuery[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerQuery);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
$data = curl_exec($ch);
/*var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
echo "<br><br><br>".$data;*/
//echo curl_errno($ch);
$response = json_decode($data);
echo "url: " . $response->url."<br />";
echo "id: " . $response->id."<br />";
curl_close($ch);