DEV Community

Leon Wei
Leon Wei

Posted on • Originally published at aisaastemplate.com

A Django Developer's Guide to Leveraging Google's Indexing API

Originally published at AISaaSTemplate

In the dynamic landscape of web development, keeping your site's content fresh and swiftly indexed by Google is crucial for maintaining visibility and relevance. For Django developers, integrating Google's Indexing API into your projects can significantly streamline this process, ensuring that your content is quickly discoverable through Google Search. This guide provides a comprehensive walkthrough on setting up and using Google's Indexing API within your Django applications, empowering you to programmatically request Google to index your website's content.

Step 1: Project Setup in Google API Console

Before diving into code, the initial step involves setting up your environment to communicate with Google's Indexing API. This setup is a one-time process that grants your application the necessary permissions to interact with Google services.

Creating a Google API Console Project

  1. Navigate to the Google API Console: Start by accessing the Google API Console. Here, you're greeted with an intuitive interface to manage Google APIs across your projects.

  2. Create a New Project: Click on the project drop-down menu at the top of the page and select "New Project". Provide a meaningful name that represents your client or the specific application you're working on.

  3. Enable the Indexing API: Once your project is created, use the search bar to find the "Indexing API". Select it and click "Enable" to activate the API for your project.

Setting Up a Service Account

A service account acts as a non-human user that your application can use to authenticate with Google and make authorized API calls.

  1. Access Service Accounts: In your Google API Console project, navigate to the "Service accounts" section.

  2. Create a New Service Account: Click on "Create Service Account" and fill in the required details. The service account ID can be the default one or a custom ID that you specify.

  3. Generate a JSON Key: After creating your service account, you'll need to generate a private key. Choose JSON as the format, which will download the key to your machine. Keep this key secure as it will be used to authenticate your API requests.

Step 2: Verify Site Ownership with Google Search Console

To use the Indexing API, Google requires verification of site ownership. This step ensures that only authorized users can request indexing for a given website.

  1. Verify Your Site: If you haven't already, add your site to Google Search Console and complete the verification process. This can be done through various methods, such as HTML file upload, DNS record, or through your domain registrar.

  2. Add Your Service Account as a Site Owner: In the Search Console, navigate to the property (site) you've verified. Under the "Settings" menu, find the "Users and permissions" section and add your service account email as an owner. This email is found in the JSON key you downloaded earlier.

Step 3: Integrating with Your Django Project

With the setup complete, it's time to integrate the Indexing API within your Django project. This involves programmatically generating OAuth tokens and making API requests to notify Google of new or updated content.

Generating OAuth Tokens

Google provides libraries in various languages to handle authentication. For Python and Django, the google-auth library can be used to generate OAuth tokens using your service account's JSON key.

  1. Install google-auth: Use pip to install the google-auth library if you haven't already.

  2. Generate Tokens: Utilize the library to load your service account key and generate OAuth tokens. These tokens are used to authenticate your API requests.

Making API Requests

With authentication taken care of, you can now make requests to the Indexing API to notify Google of new or updated pages.

  1. Prepare Your API Request: Construct a POST request to the Indexing API's endpoint, including the URL of the page you wish to be indexed. Ensure your OAuth token is included in the request headers for authentication.

  2. Handle Responses: Google's API will respond with the status of your request. Successful requests will return a status code indicating the request has been accepted for processing.

Sample code:

# Standard library imports
import os
import datetime

# Django-specific imports
import django
from django.urls import reverse
from django.conf import settings

# Third-party imports
import google.auth
from google.auth.transport.requests import Request
from google.oauth2.service_account import Credentials

# Local imports
from your_project.blog.models import Blog

# Setup Django environment
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
django.setup()

# Constants
SCOPES = ["https://www.googleapis.com/auth/indexing"]
ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
JSON_KEY_FILE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "your-project-12345.json")

# Initialize and authorize service account credentials
credentials = Credentials.from_service_account_file(JSON_KEY_FILE_PATH, scopes=SCOPES)
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)


def get_blog_urls(num_blogs=200):
    """
    The maximum number of request you can send per day is 200 by default.
    You can adjust this number based on your daily quota or contact Google for a higher quota.
    Generates a list of blog URLs to be indexed, based on a calculated daily offset.
    """    
    blogs = Blog.objects.order_by('-id')[:num_blogs]
        
    base_url = settings.SITE_URL  # Assuming SITE_URL is defined in your Django settings

    urls = [f"{base_url}{reverse('blog_detail', args=[blog.slug])}" for blog in blogs]
    return urls


def index_urls():
    """
    Indexes URLs by making POST requests to the Google Indexing API.
    """
    url_list = get_blog_urls()
    print("Indexing URLs:", url_list)

    for url in url_list:
        body = {
            "url": url,
            "type": "URL_UPDATED"
        }
        response = authed_session.post(ENDPOINT, json=body)
        print(response.status_code, response.text)


if __name__ == "__main__":
    index_urls()

Conclusion

Integrating Google's Indexing API into your Django projects offers a proactive approach to SEO, ensuring that your content is indexed promptly by Google. By following the steps outlined in this guide, you can set up the necessary infrastructure, authenticate your applications, and begin making indexing requests programmatically. This not only enhances your site's visibility on Google Search but also gives you greater control over how and when your content is indexed.

Top comments (0)