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
gcloud
command 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/blogger
scope 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