gayan balasooriya gayan balasooriya - 28 days ago 19
PHP Question

google api v3 blogger post insert php ajax

I have tried to get blogger blog info by using the api.

$blogger = new Google_BloggerService($client);
$data = $blogger->blogs->getByUrl(array('url'=>'http://puwaruwa.blogspot.com/'));


and it worked fine and gave me the details.

then I tried to insert a post as below

<?php

$token = json_decode($token, true); //already got the token

?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$.ajax({
type:"POST",
url: "https://www.googleapis.com/blogger/v3/blogs/55555555555555/posts/",
Authorization: "OAuth <?php echo $token['access_token']; ?>",
data: {"kind": "blogger#post",
"blog": {
"id": "555555555555555"
},
"title": "A new post",
"content": "With <b>exciting</b> content..."},
dataType: 'json',
contentType: 'application/json',
success:function(data){alert(data);}
});

</script>


but it gives me

{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}


please help me to insert a post to blogger blog through api v3 using php.

Answer

It was simple than I was trying to post on blogger. I am presenting here only the php code that does the post and it was working fine for me.

$blogger = new Google_BloggerService($client);    
$mypost = new Google_Post();
$mypost->setTitle('this is a test 1 title');
$mypost->setContent('this is a test 1 content');
$data = $blogger->posts->insert('5555555555555555555', $mypost);

thanks.

full detailed posting is at http://gayanonline.blogspot.com/2013/03/google-api-v3-with-php-using-blogger.html

Comments