Skip to content

Commit

Permalink
Use Google API creds from env secret first
Browse files Browse the repository at this point in the history
  • Loading branch information
tewson committed Mar 14, 2024
1 parent 3a9d348 commit 8077388
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/astro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ concurrency:
env:
BUILD_PATH: "." # default value when not using subfolders
# BUILD_PATH: subfolder
GOOGLE_API_CREDENTIALS: ${{ secrets.GOOGLE_API_CREDENTIALS }}

jobs:
build:
Expand Down
16 changes: 15 additions & 1 deletion src/google-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ const SCOPES = ["https://www.googleapis.com/auth/spreadsheets.readonly"];
const TOKEN_PATH = path.join(process.cwd(), "token.json");
const CREDENTIALS_PATH = path.join(process.cwd(), "credentials.json");

function loadSavedCredentialsFromEnvIfExist() {
try {
const content = import.meta.env.GOOGLE_API_CREDENTIALS;
const credentials = JSON.parse(content);
return google.auth.fromJSON(credentials) as Auth.OAuth2Client;
} catch (err) {
return null;
}
}

async function loadSavedCredentialsIfExist() {
try {
const content = await fs.readFile(TOKEN_PATH);
Expand All @@ -39,7 +49,11 @@ async function saveCredentials(client: Auth.OAuth2Client) {
}

export async function authorize() {
let client = await loadSavedCredentialsIfExist();
let client = loadSavedCredentialsFromEnvIfExist();
if (client) {
return client
}
client = await loadSavedCredentialsIfExist();
if (client) {
return client;
}
Expand Down

0 comments on commit 8077388

Please sign in to comment.