Skip to content

Commit

Permalink
using non deprecated Credentials API
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jan 20, 2025
1 parent 1c7bd47 commit 8e97bb8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type Google_Sheets
Google Admin Console when generating a key.
initialize : File|Enso_Secret -> Google_Sheets
initialize secret_file =
app_name = 'Enso'
base_credential = case secret_file of
secret : Enso_Secret ->
# TODO to avoid passing credentials around, prod should probably create instance of Sheets and return it here
Expand All @@ -41,9 +40,7 @@ type Google_Sheets
stream.with_java_stream is->
GoogleCredential.fromStream is
credential = base_credential.createScoped (Collections.singleton SheetsScopes.SPREADSHEETS)
http_transport = GoogleNetHttpTransport.newTrustedTransport
json_factory = GsonFactory.getDefaultInstance
service = Sheets.Builder.new http_transport json_factory credential . setApplicationName app_name . build
service = GoogleSheetsHelpers.createService credential
Google_Sheets.Service service

## ICON data_input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package org.enso.google;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.auth.oauth2.GoogleCredentials;
import org.enso.base.enso_cloud.ExternalLibrarySecretHelper;
import org.enso.base.enso_cloud.HideableValue;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class GoogleOAuthSecretReader {
public static GoogleCredential createCredentialFromSecretValue(HideableValue secretValue) {
public static GoogleCredentials createCredentialFromSecretValue(HideableValue secretValue) {
String payload = ExternalLibrarySecretHelper.resolveValue(secretValue);
ByteArrayInputStream stream = new ByteArrayInputStream(payload.getBytes());
try {
return GoogleCredential.fromStream(stream);
return GoogleCredentials.fromStream(stream);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.enso.google;

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;

public class GoogleSheetsHelpers {
Expand All @@ -16,4 +21,14 @@ public static List<List<Object>> getSheetRange(Sheets service, String sheetId, S
.execute()
.getValues();
}

public static Sheets createService(GoogleCredentials credentials) throws GeneralSecurityException, IOException {
var credentialsAdapter = new HttpCredentialsAdapter(credentials);
Sheets.Builder builder = new Sheets.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
GsonFactory.getDefaultInstance(),
credentialsAdapter
).setApplicationName("Enso");
return builder.build();
}
}

0 comments on commit 8e97bb8

Please sign in to comment.