根据文档,我们需要使用下一种格式:
POST https://www.googleapis.com/blogger/v3/blogs/8070105920543249955/posts/
Authorization: /* OAuth 2.0 token here */
Content-Type: application/json
{
"kind": "blogger#post",
"blog": {
"id": "8070105920543249955"
},
"title": "A new post",
"content": "With <b>exciting</b> content..."
}
因此必须执行以下步骤:
1)您必须在blogger \ google
中获得授权
2)使用限时令牌进行后期查询。
因此,要进行身份验证的步骤如下:
1)获取身份验证令牌的代码(此代码在重定向后出现)
2)发送auth_id,代码等以获取auth_token
在这里,我们获得带有重定向的身份验证代码:
//main.php
$url = "https://accounts.google.com/o/oauth2/auth";
$params = array(
"response_type" => 'code',
"client_id" => '<AUTH_ID>.apps.googleusercontent.com',
"redirect_uri" => 'http://example.com/authCallback.php',
"scope" => 'https://www.googleapis.com/auth/blogger');
$request_to = $url.'?'.http_build_query($params);
header("Location: ".$request_to);
在这里,我们获得auth_token以便在Blogger中发布:
//authCallback.php
if(isset($_GET['code'])) {
// Get an access token
$code = $_GET['code'];
$url = 'https://accounts.google.com/o/oauth2/token';
$apikey = '<API_KEY>';
$blogId = '<BLOG_ID>';
$params = array(
"code" => $code,
"client_id" => urlencode("<ID>.apps.googleusercontent.com"),
"client_secret" => urlencode("<SECRET>"),
"redirect_uri" => urlencode("http://example.com/authCallback.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);
$response = json_decode($data);
$accessToken = $response->access_token;
}
最后我们有了auth_token,现在我们可以使用文档中的示例了。
但是重定向中断了我的应用程序的工作循环,我需要避免这种情况。
所以我想知道如何在不重定向的情况下获取$ accessToken?