Stanimir
Stanimir

Reputation: 23

"Please open the following address in your browser..." issue with google sheets api and spring boot

I want to add functionality for reading google sheet in my spring boot project. I have followed guides from https://www.baeldung.com/google-sheets-java-client and https://developers.google.com/sheets/api/quickstart/java, created a google web project, (oAuth) JSON file with:

redirect_uris":["http://localhost:8989/Callback"]

Configuration GoogleAuthorizeUtil class is as follows:

public class GoogleAuthorizeUtil {
       
    public static Credential authorize() throws IOException, GeneralSecurityException {
        
        String TOKENS_DIRECTORY_PATH = "tokens";
            
        InputStream in = GoogleAuthorizeUtil.class.getResourceAsStream("/google-sheets-client-secret.json");
            
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));

        List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
        
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow
                    .Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), clientSecrets, scopes)
                    //.setDataStoreFactory(new MemoryDataStoreFactory())
                    .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                    .setAccessType("offline")
                    .build();
        
        LocalServerReceiver localServerReceiver = new LocalServerReceiver.Builder().setPort(8989).build();
            
        Credential credential = new AuthorizationCodeInstalledApp(flow, localServerReceiver).authorize("user");

        return credential;
        
    }

}

SheetsServiceUtil like:

public class SheetsServiceUtil {
    
    private static final String APPLICATION_NAME = "Google Sheets Read";

    public static Sheets getSheetsService() throws IOException, GeneralSecurityException {
        
        Credential credential = GoogleAuthorizeUtil.authorize();
        return new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), credential)
                .setApplicationName(APPLICATION_NAME)
                .build();
    
    }

}

and part of service for reading sheets

@Service
public class GSheeetService {
    
    MessageResponse readSheet() throws IOException, GeneralSecurityException {
        
        String spreadsheetId = "realsheetid"; 
        
        String range = "Sheet1";

        Sheets sheetsService = SheetsServiceUtil.getSheetsService();
        Sheets.Spreadsheets.Values.Get request =
            sheetsService.spreadsheets().values().get(spreadsheetId, range);
//...

And after "first" run of application and after, for example, one hour (or more) STS console shows:

"Please open the following address in your browser: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=*clientIdNumber*.apps.googleusercontent.com&redirect_uri=http://localhost:8989/Callback&response_type=code&scope=https://www.googleapis.com/auth/spreadsheets"

When I open the address in the browser I got the message "Received verification code. You may now close this window." and service for reading sheets works fine for a while. After approximately two hours, the console again gives "Please open the following address in your browser" etc.

Any idea? (I have read a bunch of related topics)

Upvotes: 2

Views: 2521

Answers (1)

Iamblichus
Iamblichus

Reputation: 19339

As DaImTo mentioned, the code you are using is for an installed application, while your credentials are for a web application.

Therefore, depending on what kind of application you want to have, you'd need to change different things:

  • If you want to have an installed application, you'd have to create credentials for this kind of application and use those instead of web application credentials.
  • If you want to have a web application, you'd have to change your code so that it's appropriate for this kind of application. Here you can see several complete examples in other languages, and here you can see an example in Java.

Further reading:

Upvotes: 0

Related Questions