Use Blogger API from its Java client
- Get link
- X
- Other Apps
Google Blogger provides an API to create and edit blog posts on Blogger. See their Introduction here.
It also provides API client libraries for various languages including Java, JS, Python, etc.
As I haven't found a sample app for the Java client, I'm writing this with the hope that this can save someone's time.
In this post, I'm going to create a new blog post with a title "My Test Post".
Prerequisite
- You have a blog on Blogger (in this post, it's
https://blog.example.org) - You have a GCP project with Billing enabled.
- You have
gcloudcommand installed. cf. here.
Steps
- Enable Blogger API
- Create OAuth 2.0 credentials
- Create OAuth consent screen
- Create a Java app that update your blog
- Get a new OAuth token
- Run the Java app to create a new post on your blog
Step 1. Enable Blogger API
Go to Blogger API page, make sure the correct project is selected, then enable the API.
https://console.cloud.google.com/apis/api/blogger.googleapis.com/overview
Step 2. Create OAuth 2.0 credentials
-
Go to API credentials page, make sure the correct project is selected
https://console.developers.google.com/apis/credentials
-
Create a new OAuth client ID, with "Application type" Other.
-
Download the credentials of the newly created item, confirm that the credential contains the following information:
{ "installed": { "client_id": "...", "client_secret": "...", ... } }
Step 3. Create OAuth consent screen
- Go to: https://console.developers.google.com/apis/credentials/consent
- Make sure you have valid "Support email" is entered.
- Add
../auth/bloggerscope to allow managing your blogger account. - "Save"
Step 4. Create a Java app that update your blog
// src/main/java/org/example/App.java
package org.example;
import com.google.api.services.blogger.Blogger;
import com.google.api.services.blogger.BloggerRequestInitializer;
import com.google.api.services.blogger.model.Blog;
import com.google.api.services.blogger.model.Post;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.security.GeneralSecurityException;
public class App {
public static void main(String[] argv) throws GeneralSecurityException, IOException {
Blogger blogger = getBlogger();
Post newPost = new Post().setTitle("My Test Post").setContent("With <b>exciting</b> content...");
Blog blog = blogger.blogs().getByUrl("https://blog.example.org/").execute();
Blogger.Posts.Insert command = blogger.posts().insert(blog.getId(), newPost);
System.out.println(command.executeUnparsed().parseAsString());
}
private static Blogger getBlogger() throws GeneralSecurityException, IOException {
Blogger.Builder builder = new Blogger.Builder(
com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(),
new com.google.api.client.json.jackson2.JacksonFactory(),
new HttpCredentialsAdapter(GoogleCredentials.getApplicationDefault())
);
return builder.setBloggerRequestInitializer(new BloggerRequestInitializer()).build();
}
}
// build.gradle
plugins {
id 'java'
id 'application'
}
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.apis:google-api-services-blogger:v3-rev20190917-1.30.9'
compile 'com.google.auth:google-auth-library-oauth2-http:0.20.0'
}
mainClassName = 'org.example.App'
Step 5. Get a new OAuth token
Execute the following command. It will seek your approval on a browser
unless you use --no-launch-browser option.
gcloud auth application-default login \
--scopes='https://www.googleapis.com/auth/blogger' \
--client-id-file downloaded-oauth-client-id.json
This will save an Application Default Credentials (ADC) at $HOME/.config/gcloud/application_default_credentials.json.
Step 6. Run the Java app to create a new post on your blog
Make the credential available to the app through an environment variable, and run the app.
export GOOGLE_APPLICATION_CREDENTIALS=~/.config/gcloud/application_default_credentials.json
gradle run
Now open your blog and confirm the new post is published.
- Get link
- X
- Other Apps
Comments
Is Casino Casino Fake? Check Out Our True Story
ReplyDeleteCasino is legit but there is 밀양 출장샵 no indication 대구광역 출장샵 of fraud or fraud. Here is the best 김천 출장마사지 casino bonus codes, how 밀양 출장안마 you can claim 성남 출장마사지 these bonus offers
Thank you for giving information about using Blogger API from its Java client
ReplyDeleteAlso check Custom API integration services
Cool stuff! Check! best porn site for hot links. Also, their quick loans are legitClick here!!
ReplyDeleteSuper helpful step-by-step example for setting up OAuth and the Java client for Blogger! Having a clear ADC workflow saves so much time. When hosting quick API demo pages or simple mockups, using a tool to create simple static pages online makes getting code live super fast.
ReplyDeleteGreat guide on setting up the Java client for Blogger API! When testing or sharing quick API payloads with teammates, option to store JSON file online makes mock testing super fast.
ReplyDeleteThe example is particularly useful because it focuses on the Java client library and demonstrates how authentication and API calls fit together in an actual application. Developers building Java-based applications can benefit from strengthening their broader Java development skills through a Java Full Stack Course, especially when working with APIs and backend application logic.
ReplyDeleteI also like that the article addresses a practical gap by providing a Java client example rather than simply referring readers to the API documentation. The sequence from enabling the Blogger API through obtaining an OAuth token and creating a post gives the tutorial a clear progression. For developers interested in applying Java skills to full-stack applications, a Spring Boot ReactJS Full Stack Course can provide additional context for building applications around Java-based backend services.
ReplyDelete