I'm trying to integrate GSheets API in one of my APIs.
My usecase is as follows. My API invokes the Gsheet API to read data from a sheet and then there's some logic thereafter.
I've configured redirect uri as http://localhost
in Google API console.
When I run my application it tries to redirect to a different URI ...redirect_uri=http://localhost:49868/Callback&response_type=code&scope=https://www.googleapis.com/auth/spreadsheets
This is my code
class SheetsServiceUtil @Inject constructor(
private val googleAuthorizeUtil: GoogleAuthorizeUtil
){
companion object {
private const val APPLICATION_NAME = "<my_app_name>";
}
fun getSheetsService() : Sheets {
val credential : Credential = googleAuthorizeUtil.authorizeClient()
return Sheets.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(), credential)
.setApplicationName(APPLICATION_NAME)
.build()
}
}
class GoogleAuthorizeUtil {
fun authorizeClient() : Credential {
val filePointer : Reader = File("<my_file_location>.json").bufferedReader()
val clientSecrets : GoogleClientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), filePointer)
var scopes = mutableListOf(SheetsScopes.SPREADSHEETS)
val flow : GoogleAuthorizationCodeFlow = GoogleAuthorizationCodeFlow.Builder(
GoogleNetHttpTransport
.newTrustedTransport(), JacksonFactory.getDefaultInstance(), clientSecrets, scopes)
.setDataStoreFactory(MemoryDataStoreFactory())
.setAccessType("offline").build()
return AuthorizationCodeInstalledApp(flow, LocalServerReceiver()).authorize("user")
}
}