A Django Developer's Guide to Leveraging Google's Indexing API
Tags: seo, django, google
Published: 2024-03-25 17:28:39

Originally published at [AISaaSTemplate](https://aisaastemplate.com)

<p>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.</p>
<h2 id="step-1-project-setup-in-google-api-console">Step 1: Project Setup in Google API Console</h2>
<p>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.</p>
<h3>Creating a Google API Console Project</h3>
<ol>
<li>
<p><strong>Navigate to the Google API Console</strong>: Start by accessing the <a href="https://console.developers.google.com/" target="_new">Google API Console</a>. Here, you're greeted with an intuitive interface to manage Google APIs across your projects.</p>
</li>
<li>
<p><strong>Create a New Project</strong>: 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.</p>
</li>
<li>
<p><strong>Enable the Indexing API</strong>: 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.</p>
</li>
</ol>
<h3>Setting Up a Service Account</h3>
<p>A service account acts as a non-human user that your application can use to authenticate with Google and make authorized API calls.</p>
<ol>
<li>
<p><strong>Access Service Accounts</strong>: In your Google API Console project, navigate to the "Service accounts" section.</p>
</li>
<li>
<p><strong>Create a New Service Account</strong>: 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.</p>
</li>
<li>
<p><strong>Generate a JSON Key</strong>: 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.</p>
</li>
</ol>
<h2 id="step-2-verify-site-ownership-with-google-search-console">Step 2: Verify Site Ownership with Google Search Console</h2>
<p>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.</p>
<ol>
<li>
<p><strong>Verify Your Site</strong>: 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.</p>
</li>
<li>
<p><strong>Add Your Service Account as a Site Owner</strong>: 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.</p>
</li>
</ol>
<h2 id="step-3-integrating-with-your-django-project">Step 3: Integrating with Your Django Project</h2>
<p>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.</p>
<h3>Generating OAuth Tokens</h3>
<p>Google provides libraries in various languages to handle authentication. For Python and Django, the <code>google-auth</code> library can be used to generate OAuth tokens using your service account's JSON key.</p>
<ol>
<li>
<p><strong>Install <code>google-auth</code></strong>: Use pip to install the <code>google-auth</code> library if you haven't already.</p>
</li>
<li>
<p><strong>Generate Tokens</strong>: Utilize the library to load your service account key and generate OAuth tokens. These tokens are used to authenticate your API requests.</p>
</li>
</ol>
<h3>Making API Requests</h3>
<p>With authentication taken care of, you can now make requests to the Indexing API to notify Google of new or updated pages.</p>
<ol>
<li>
<p><strong>Prepare Your API Request</strong>: 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.</p>
</li>
<li>
<p><strong>Handle Responses</strong>: 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.</p>
</li>
</ol>
<h2 id="sample-code">Sample code:</h2>
<pre class="language-python"><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()
</code></pre>
<h2 id="conclusion">Conclusion</h2>
<p>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.</p>