-1

I am using Spring boot 3.3 with below configuration

Application.yaml :----- spring: security: oauth2: client: registration: google: client-id: xxxx client-secret: xx scope: profile, email, openid authorization-grant-type: authorization_code redirect-uri: http://localhost:8080/login/oauth2/code/google client-name: Google provider: google: #authorization-uri: https://accounts.google.com/o/oauth2/v2/auth authorization-uri: https://accounts.google.com/o/oauth2/auth?access_type=offline token-uri: https://oauth2.googleapis.com/token user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo

Service class:----------------

@Service public class OAuth2TokenService {

@Autowired
private OAuth2AuthorizedClientService authorizedClientService;

@Autowired
private ClientRegistrationRepository clientRegistrationRepository;

private final RestTemplate restTemplate = new RestTemplate();

public OAuth2AccessToken refreshToken(Authentication authentication) {
    String principalName = "";
     if (authentication instanceof OAuth2AuthenticationToken) {
         OAuth2AuthenticationToken oauth2Token = (OAuth2AuthenticationToken) authentication;
         
         // Get the OAuth2User (OidcUser for OpenID Connect, OAuth2User for others)
         OidcUser oidcUser = (OidcUser) oauth2Token.getPrincipal();  // For OIDC (Google, etc.)
         
         // Get the principal's name (typically the name attribute)
          principalName = oidcUser.getName();  // You can also use oidcUser.getEmail(), oidcUser.getFullName(), etc.
         System.out.println( "Principal Name: " + principalName);
     }
    // Retrieve the OAuth2AuthorizedClient using the registrationId
    final var registrationId = "google";
    OAuth2AuthorizedClient authorizedClient = authorizedClientService.loadAuthorizedClient(registrationId, principalName);
    
    // Get the refresh token from the authorized client
    OAuth2RefreshToken refreshToken = authorizedClient.getRefreshToken();

    **if (refreshToken == null ) {
        throw new IllegalArgumentException("No refresh token found");
    }**

1 Answer 1

0

Find related post , now my issue resolved now

Not receiving Google OAuth refresh token

authorization-uri: https://accounts.google.com/o/oauth2/v2/auth?prompt=consent&access_type=offline

Needs to add prompt ,access_type params in your request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.