Demo Application
Learn how to integrate the Blogger API endpoints into a simple Django application.
We'll cover the following
In this lesson, we'll look at a simple Django-based web application created by integrating Blogger APIs. This application will allow us to view our blogs as well as create and view blog pages and blog posts.
Run the application
Step 1: Click the "Run" button to start the application. Click the URL next to "Your app can be found at:" in the widget below to open the application in a new tab.
Step 2: By default, you'll see the application's homepage with three buttons: "Blogs," "Pages," and "Posts."
The "Blogs" button will redirect you to the blogs page where you'll be able to view a list of all the blogs created by the specified user.
The "Pages" button will redirect you to a page where you'll be asked to select a blog that you would like to retrieve the blog pages for. After you select the blog from the dropdown and click the "Get Blog Pages" button, you'll be redirected to a new page where you'll be able to view a list of all the blog pages associated with your selected blog. Moreover, you'll be provided with a button to redirect to page creation and a button to redirect back to the homepage.
Similarly, the "Posts" button will redirect you to a page where you'll be asked to select a blog that you would like to retrieve the blog posts for. After you select the blog from the dropdown and click the "Get Blog Posts" button, you'll be redirected to the posts page where you'll be able to view a list of all the blog posts associated with your selected blog. Moreover, you'll be provided with a button to redirect to the post creation page and a button to redirect back to the homepage.
You can also switch between pages using the top navigation bar. This bar also provides a button named "User Profile" to redirect to the user profile page.
Application code
from django.shortcuts import render import requests from datetime import datetime headers = { 'Authorization': 'Bearer {{ACCESS_TOKEN}}', 'Accept': 'application/json' } def getResponse(response): if response.status_code == 200: return True, response.json() else: return False, f'{response.status_code}: {response.reason}' def getBlogs(): url = 'https://blogger.googleapis.com/v3/users/{{USER_ID}}/blogs' response = requests.get(url, headers=headers) isSuccess, data = getResponse(response) blogs = {} if isSuccess: for blog in data['items']: blogs[blog['name']] = blog['id'] return blogs def dashboard(request): return render(request, 'blogger_apiv3/dashboard.html') def createPage(request): if request.method == 'POST': blogId = request.POST.get("blogId") data = { "title": request.POST.get("title"), "content": request.POST.get("content"), } url = 'https://blogger.googleapis.com/v3/blogs/' + blogId + '/pages' response = requests.post(url, headers=headers, json=data) isSuccess, message = getResponse(response) return render(request,'blogger_apiv3/createPage.html', {'isSuccess': isSuccess, 'message': message, 'blogs': getBlogs()}) return render(request, 'blogger_apiv3/createPage.html', {'blogs': getBlogs()}) def createPost(request): if request.method == 'POST': blogId = request.POST.get("blogId") data = { "title": request.POST.get("title"), "content": request.POST.get("content"), } url = 'https://blogger.googleapis.com/v3/blogs/' + blogId + '/posts' response = requests.post(url, headers=headers, json=data) isSuccess, message = getResponse(response) return render(request,'blogger_apiv3/createPost.html', {'isSuccess': isSuccess, 'message': message, 'blogs': getBlogs()}) return render(request, 'blogger_apiv3/createPost.html', {'blogs': getBlogs()}) def listBlogs(request): url = 'https://blogger.googleapis.com/v3/users/{{USER_ID}}/blogs' response = requests.get(url, headers=headers) isSuccess, data = getResponse(response) allBlogs = [] count = 0 if isSuccess: for blog in data['items']: tempBlog = dict() tempBlog['id'] = blog['id'] tempBlog['name'] = blog['name'] tempBlog['noOfPages'] = blog['pages']['totalItems'] tempBlog['noOfPosts'] = blog['posts']['totalItems'] tempBlog['publishDate'] = datetime.strptime(blog['published'][0:19], "%Y-%m-%dT%H:%M:%S").date() tempBlog['status'] = blog['status'] tempBlog['url'] = blog['url'] allBlogs.append(tempBlog) count += 1 data = allBlogs return render(request,'blogger_apiv3/listBlogs.html', {'isSuccess': isSuccess, 'data': data, 'count': count}) def listPages(request): if request.method == 'POST': blogId = request.POST.get("blogId") url = 'https://blogger.googleapis.com/v3/blogs/' + blogId + '/pages' response = requests.get(url, headers=headers) isSuccess, data = getResponse(response) allPages = [] count = 0 if isSuccess: for page in data['items']: tempPage = dict() tempPage['id'] = page['id'] tempPage['blogId'] = page['blog']['id'] tempPage['publishDate'] = datetime.strptime(page['published'][0:19], "%Y-%m-%dT%H:%M:%S").date() tempPage['title'] = page['title'] tempPage['url'] = page['url'] allPages.append(tempPage) count += 1 data = allPages return render(request,'blogger_apiv3/listPages.html', {'isSuccess': isSuccess, 'data': data, 'count': count}) return render(request, 'blogger_apiv3/listPages.html', {'blogs': getBlogs()}) def listPosts(request): if request.method == 'POST': blogId = request.POST.get("blogId") url = 'https://blogger.googleapis.com/v3/blogs/' + blogId + '/posts' response = requests.get(url, headers=headers) isSuccess, data = getResponse(response) allPosts = [] count = 0 if isSuccess: for post in data['items']: tempPost = dict() tempPost['id'] = post['id'] tempPost['blogId'] = post['blog']['id'] tempPost['publishDate'] = datetime.strptime(post['published'][0:19], "%Y-%m-%dT%H:%M:%S").date() tempPost['title'] = post['title'] tempPost['commentCount'] = post['replies']['totalItems'] tempPost['url'] = post['url'] allPosts.append(tempPost) count += 1 data = allPosts return render(request,'blogger_apiv3/listPosts.html', {'isSuccess': isSuccess, 'data': data, 'count': count}) return render(request, 'blogger_apiv3/listPosts.html', {'blogs': getBlogs()}) def userProfile(request): url = 'https://blogger.googleapis.com/v3/users/{{USER_ID}}' response = requests.get(url, headers=headers) isSuccess, data = getResponse(response) user = dict() if isSuccess: user['id'] = data['id'] user['name'] = data['displayName'] user['about'] = data['about'] user['url'] = data['url'] user['creationDate'] = datetime.strptime(data['created'][0:19], "%Y-%m-%dT%H:%M:%S").date() user['country'] = data['locale']['country'] if data['locale']['country'] else '-' user['language'] = data['locale']['language'] user['languageVariant'] = data['locale']['variant'] if data['locale']['variant'] else '-' data = user return render(request, 'blogger_apiv3/userProfile.html', {'isSuccess': isSuccess, 'data': data})
Code explanation
In the code above in the blogger_apiv3/views.py
file:
Line 34: We use the
createPage()
function to create a blog page.Line 53: We use the
createPost()
function to create a blog post.Line 72: We use the
listBlogs()
function to retrieve a list of blogs for the given user.Line 98: We use the
listPages()
function to retrieve a list of blog pages for the given user.Line 127: We use the
listPosts()
function to retrieve a list of blog posts for the given user.Line 157: We use the
userProfile()
function to retrieve the profile of the user.