Wednesday, June 22, 2022

interview

 

1. How to log all the requests and responses for all restful APIs?

2. How to log all the requests and responses for the SOAP web services?

3. How to disable the log in PRD?

Spring cloud config

context-path for JBoss/Spring boot

what's the advantage/disadvantage of Spring boot?

what's CORS, how would you in Spring boot?

Exception Spring boot

Spring Session + Redis 来实现 session 共享 micro services

RestTemplate/Apache http client/webclient (spring 5)

How and why you can get a POJO object instead of a String?

Say you developed a new endpoint, how do you test it? If there's unexpected issue, how would you debug/fix?


Friday, June 10, 2022

Send gmail using OAuth2 - Java

 1.

Login to https://console.cloud.google.com/ to create the project and the client_id, oauth consent screen...

2.

Run the oauth2.py to get the refresh token.

I updated the file to support python 3.

3.

From Java,

call the below to exchange back the access token:

public static String getNewToken(String refreshToken, String clientId, String clientSecret) throws IOException {

List<String> scopes = new ArrayList<>();

TokenResponse tokenResponse = new GoogleRefreshTokenRequest(new NetHttpTransport(), new JacksonFactory(),

refreshToken, clientId, clientSecret).setScopes(scopes).setGrantType("refresh_token").execute();

return tokenResponse.getAccessToken();

}

4. With the access token, call the below to send email:

SMTPTransport transport = new SMTPTransport(session, null);

transport.connect(SMTP_SERVER_HOST, FROM_USER_EMAIL, null);

transport.issueCommand(

"AUTH XOAUTH2 " + new String(BASE64EncoderStream.encode(

String.format("user=%s\1auth=Bearer %s\1\1", FROM_USER_EMAIL, newAccessToken).getBytes())),

235);

transport.sendMessage(msg, msg.getAllRecipients());

Refer:

https://hellokoding.com/sending-email-through-gmail-smtp-server-with-java-mail-api-and-oauth-2-authorization/

https://www.youtube.com/watch?v=-rcRf7yswfM