sábado, 10 de junio de 2017

Google Blogger API V3 PHP usando OAuth 2.0

Insertar Nueva Entrada desde php

Fue muy difícil para mí entender cómo funcionan las API de Google en el primer punto y tardé unos días en descubrirlo. Pero después de un poco de trabajo llegue a la conclusion de que es muy fácil. les dejo un ejemplo sencillo para los que utilizan php.
Nota: primero antes que nada deben crear los credenciales del token dentro de google OAuth 2.0 Playground

en scope se debe colocar https://www.googleapis.com/auth/blogger
<?php

$blogId = "YOU_BLOG_ID";
 
$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();
$accessToken = 'YOU_OAuth_2.0_Playground_ACCESS_token';
 
        $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);
echo $data;
//echo curl_errno($ch);
$response = json_decode($data);
 
        curl_close($ch);


?>

No hay comentarios:

Publicar un comentario