1

我在如何在 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);
4

1 回答 1

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);
于 2013-11-08T20:38:54.447 回答