我在如何在 Blogger 中发布帖子时遇到了麻烦,根据他们的文档,查询必须如下所示:
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);