Create a Blogger Auto-Poster with Python (.exe) - Part 1: The Foundation
Welcome to the first part of our course! In this section, we will lay the essential groundwork for our application. Our goal is to create a simple Python script that can securely connect to your Google account and publish a hard-coded test post to one of your Blogger blogs. This will confirm our connection to the Blogger API is working perfectly.
What You'll Achieve in Part 1:
- Set up a Google Cloud project and get the necessary API credentials.
- Write a Python script to handle secure authentication using OAuth 2.0.
- Successfully post a "Hello, World!" message to your blog programmatically.
Step 1: Google Cloud API Setup
This is the most critical step. We need to tell Google that we're building an application that needs to access Blogger on our behalf.
- Go to the Google Cloud Console: https://console.cloud.google.com/
- Create a New Project: If you don't have one already, click the project dropdown at the top and select "New Project". Name it something like "Blogger Auto Poster" and click Create.
- Enable the Blogger API: In the search bar at the top, type "Blogger API v3" and select it. Click the "Enable" button.
- Create Credentials:
- On the left menu, go to "APIs & Services" > "Credentials".
- Click "+ CREATE CREDENTIALS" at the top and select "OAuth client ID".
- You may be prompted to "Configure Consent Screen". Choose "External" and fill in the required fields (App name, your email). Don't worry about the advanced settings for now. Save and continue.
- For "Application type", select "Desktop app". This is very important.
- Give it a name, like "Blogger Poster Script".
- Click "Create".
- Download Your Credentials: A pop-up will appear. Click "DOWNLOAD JSON". Rename this downloaded file to
client_secret.json
and save it in a new folder on your computer where you will create your Python script.
client_secret.json
file is sensitive. Do not share it publicly or commit it to a public GitHub repository.Step 2: Setting Up Your Python Environment
We'll use a virtual environment to keep our project's dependencies separate.
- Open a terminal or command prompt and navigate to your project folder.
- Create a virtual environment: python -m venv venv
- Activate it:
- Windows: .\venv\Scripts\activate
- Mac/Linux: source venv/bin/activate
- Windows:
- Install the required Google libraries:
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Step 3: The Python Script - First Post
Create a new file named poster_part1.py
in your project folder and add the following code.
blogger.com/blog/posts/1234567890123456789
, the ID is the bolded number.Hello, World! This is a test post from my new Python script. If you can see this, it means the connection to the Blogger API is working!
" body = {"title": post_title, "content": post_content} try: print(f"Publishing post to Blog ID: {BLOG_ID}...") posts = service.posts() posts.insert(blogId=BLOG_ID, body=body, isDraft=False).execute() print("🎉 Successfully published the post!") except Exception as e: print(f"An error occurred: {e}") if __name__ == '__main__': main()Step 4: Run the Script!
Make sure you've replaced "YOUR_BLOG_ID_HERE"
with your actual Blog ID.
- In your terminal, run the script: python poster_part1.py
- First Time Only: Your web browser will open, asking you to log in to your Google account and grant permission to the app.
- After you grant permission, the browser tab will close, and the script will continue.
- Check your terminal. You should see the success message!
- Check your blog. The new post should be live!
A new file named token.pickle
will have appeared in your folder. This securely stores your authorization. The script won't ask you to log in again unless this file is deleted.
Create a Blogger Auto-Poster with Python (.exe) - Part 2: The Brains
Congratulations on completing Part 1! You now have a script that can connect and post to Blogger. In this part, we'll make it intelligent. We will replace the hard-coded test message with unique, high-quality content generated by Google's Gemini AI. We will also create a configuration file to make our script flexible and easy to manage.
What You'll Achieve in Part 2:
- Set up the Google Gemini API for content generation.
- Create a configuration file to store your blog details, topics, and backlink.
- Modify the script to read the configuration, generate a unique article, and inject your backlink.
Step 1: Get Your Google Gemini API Key
We'll use Google's own AI to generate our content. This keeps everything in the Google ecosystem.
- Go to Google AI Studio: https://aistudio.google.com/
- Sign in with your Google account.
- On the left menu, click "Get API key".
- Click "Create API key in new project". A key will be generated for you.
- Copy this key and save it somewhere safe. We will use it in our configuration file. This key is secret!
Step 2: Install the Gemini Library
In your activated virtual environment, install the library for Google's Generative AI.
Step 3: Create the Configuration File
This file will hold all our settings, so we don't have to edit the Python code every time. Create a new file named config.ini
in your project folder.
Fill this file out with your own details. For now, we will just use [Blog1]
. We'll handle rotation in Part 3.
Step 4: The Upgraded Python Script
Create a new file named poster_part2.py
. We will copy the authentication logic from Part 1 and add the AI and configuration logic.
secrets.py
and add your Gemini API key to it like this: GEMINI_API_KEY = "YOUR_API_KEY_HERE"
. This keeps your key out of the main script.Step 5: Run the Upgraded Script
Make sure your config.ini
and secrets.py
files are correctly filled out.
The script will now generate a unique article based on your topic, add your backlink, and post it to your blog. Check the result!
Create a Blogger Auto-Poster with Python (.exe) - Part 3: The Final Product
You've made it to the final part! We have a powerful script, but running it from the command line every day isn't practical. In this part, we will package our Python script into a standalone Windows Executable (.exe
) file. This allows you to run it on any Windows computer without needing to install Python. We will also add the logic to rotate through your blogs and learn how to schedule it to run automatically every day.
What You'll Achieve in Part 3:
- Add rotation logic to post to a different blog each day.
- Install PyInstaller to package Python applications.
- Bundle your script and all its files into a single
.exe
file. - Learn how to use Windows Task Scheduler to automate the entire process.
Step 1: The Final Script with Rotation
Create a new file, auto_poster_final.py
. This version will include logic to read a small text file, see which blog it posted to last, and post to the next one in the list from your config.ini
.
Step 2: Install PyInstaller
PyInstaller is the tool that performs the magic of converting our .py
script into an .exe
. In your activated virtual environment, run:
Step 3: Create the Executable (.exe
)
Now, we'll use PyInstaller. We need to tell it to include our important data files.
Run this command from your terminal, in your project directory:
Command Breakdown:
--onefile
: Bundles everything into a single .exe file.--noconsole
: Prevents the black command window from popping up. It will run silently in the background (perfect for automation).--add-data "file;."
: This is crucial. It tells PyInstaller to include these necessary files in the package.--hidden-import="secrets"
: Tells PyInstaller to make sure it includes oursecrets.py
file.
After it finishes, you will find a dist
folder. Inside, you'll find your auto_poster_final.exe
file. This is your program! You can now move this `.exe` file to any Windows computer and it will run.
Step 4: Schedule with Windows Task Scheduler
This is the final step to "set it and forget it".
- Press the Windows Key and type "Task Scheduler", then open it.
- In the right-hand "Actions" pane, click "Create Basic Task...".
- Name: Give it a name like "Daily Blogger Post". Click Next.
- Trigger: Choose "Daily" and click Next. Set a time you want it to run (e.g., 9:00 AM every day).
- Action: Choose "Start a program". Click Next.
- Start a Program: Click "Browse..." and find your
auto_poster_final.exe
file in thedist
folder. - Click Next, then review the details and click "Finish".