Spring DI with different styles

Spring as a dependency injection (DI) framework allows us to define our application components' dependency tree in three different ways: XML Annotation Java Config I've written a simple app, bookstore, with the three styles and they're available in the following repository. You can take a look and see how each style would look like. It also has a version that uses no Spring beans for comparison. https://github.com/ryu1kn/spring--working-with-beans Different styles have different pros/cons. Spring lets you mix the styles; so you can change it as you go. Here is my observations on each style. XML-based configuration Pros Weak/loose coupling with Spring framework in your app code. Good for keeping the option of removing Spring later Class dependency information is centralised. Fine-grained control on the dependency definition. Changing only the dependency information doesn't require the recompilation of your app code. Cons Unless you have a good IDE

Use Blogger API from its Java client

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

  1. Enable Blogger API
  2. Create OAuth 2.0 credentials
  3. Create OAuth consent screen
  4. Create a Java app that update your blog
  5. Get a new OAuth token
  6. 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

  1. Go to API credentials page, make sure the correct project is selected

    https://console.developers.google.com/apis/credentials

  2. Create a new OAuth client ID, with "Application type" Other.

  3. 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

  1. Go to: https://console.developers.google.com/apis/credentials/consent
  2. Make sure you have valid "Support email" is entered.
  3. Add ../auth/blogger scope to allow managing your blogger account.
  4. "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.

Comments

  1. Is Casino Casino Fake? Check Out Our True Story
    Casino 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

    ReplyDelete
  2. Thank you for giving information about using Blogger API from its Java client
    Also check Custom API integration services

    ReplyDelete

Post a Comment

Popular posts from this blog

Terraform

Spring DI with different styles