Showing posts with label google. Show all posts
Showing posts with label google. Show all posts

Display Blogger feed on HTML site using RSS

This is how to parse and display a blogger feed on your website. Replace the URL of the API-call in the last script tag to change the RSS feed the script pulls. Update the months array to change the language for the date to English.


<script>
let postTitle = new Array();
let postContent = new Array();
let postUrl = new Array();
let postDate = new Array();

let months = [&quot;januari&quot;, &quot;februari&quot;, &quot;maart&quot;, &quot;april&quot;, &quot;mei&quot;, &quot;juni&quot;, &quot;juli&quot;, &quot;augustus&quot;, &quot;september&quot;, &quot;oktober&quot;, &quot;november&quot;, &quot;december&quot;];

let feedSize = 0;

function callback(apiData) {
    function load() {
        if (&quot;entry&quot; in apiData.feed) {
            let feedSize = apiData.feed.entry.length;
            for (let i = 0; i &lt; feedSize; i++) {
                let post = apiData.feed.entry[i];
                console.log(post)

                var p = post.published.$t.substring(5, 7);

                for (var f = 0; f &lt; post.link.length; f++) {
                    if (post.link[f].rel == &quot;alternate&quot;) {
                        postUrl.push(post.link[f].href);
                        break
                    }
                }

                let postDateDay = post.published.$t.substring(8, 10);
                let postDateMonth = months[parseInt(p, 10) - 1];
                let postDateYear = post.published.$t.substring(0, 4);

                postDate.push(`${postDateDay} ${postDateMonth} ${postDateYear}`);

                postTitle.push(post.title.$t);
                postContent.push(post.content.$t);
            }
        }
    }

    function display() {
        let postCount = postTitle.length;
        var currentPost = 0;
        while (currentPost &lt; postCount) {        
            document.write(`
                &lt;article class=&quot;post mt-10&quot;&gt;
                    &lt;div class=&quot;text-center my-7 max-w-xl mx-auto&quot;&gt;
                        &lt;h1 id=&quot;post-${currentPost}&quot; class=&quot;mb-0 text-4xl&quot;&gt;${postTitle[currentPost]}&lt;/h1&gt;
                        &lt;p class=&quot;my-5 bold&quot;&gt;${postDate[currentPost]}&lt;/p&gt;
                    &lt;/div&gt;
    
                    &lt;section&gt;
                        ${postContent[currentPost]}
                    &lt;/section&gt;
                &lt;/article&gt;
    
                &lt;hr class=&quot;my-7 border-none&quot;&gt;
            `);
    
            currentPost++
        }
    }
    
    load();
    display();
}
</script>
<script src="https://webdevelopment-en-meer.blogspot.com/feeds/posts/default?max-results=10&start-index=1&alt=json-in-script&callback=callback"></script>

Publish posts using Blogger API in PHP

This is how to create a blogpost using the Google Client Library for PHP.

To setup:

  1. Go to https://console.developers.google.com
  2. Add an project, and tap "Credentials"
  3. Tap "Create data" and create an Client-ID OAuth
  4. Choose the "Webapp" and enter the path to your server in the "Authorized diversion URIs" textbox
  5. Hit "Create", and hit OK
  6. Click on the Client-ID you just generated
  7. Click "Download JSON"
  8. Download the file, and rename it to client_secret.json

Use this script to create and publish posts. Make sure to first enter the path to your client_secret.json.

<?php
// This script creates a post
// Made by Robin Boers

// load Google Api
require_once '../api/vendor/autoload.php';

session_start();

$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

// Authenticate user with OAuth 
$client = new Google_Client();
$client->setAuthConfig('/path/to/client_secret.json');
$client->setApplicationName('Example App');
$client->setRedirectUri($redirect_uri);
$client->setScopes(array('https://www.googleapis.com/auth/blogger')); 

if (!isset($_SESSION['access_token']) && isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $access_token = $client->getAccessToken();
    $_SESSION['access_token'] = $access_token;
}

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    
    // First set the accesstoken
    $client->setAccessToken($_SESSION['access_token']);

    $blogger = new Google_Service_Blogger($client);

    // Get blog information
    $blog = $blogger->blogs->getByUrl('http://stupidstuffwastaken.blogspot.com'); // Replace this URI with your blog's URI
    $blogId = $blog->getId();
    $blogName  = $blog->getName();

    $postId = "651261343944639929"; // If you want to update a post

     // Creating post
     $mypost = new Google_Service_Blogger_Post();
     $mypost->setTitle("Example heading");
     $mypost->setContent("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");

     $data = $blogger->posts->insert($blogId, $mypost); // or update post like this: $data = $blogger->posts->update($blogId, $postId, $mypost);
     var_dump($data);
}
?>