Skip to content

Commit

Permalink
chore(serverless): add missing var to snippet block (#1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed Nov 8, 2021
1 parent 30bbd69 commit 5ed9105
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions samples/idtokens-serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@ function main(
url = 'https://service-1234-uc.a.run.app',
targetAudience = null
) {
if (!targetAudience) {
// Use the target service's hostname as the target audience for requests.
// (For example: https://my-cloud-run-service.run.app)
const {URL} = require('url');
targetAudience = new URL(url).origin;
}
// [START google_auth_idtoken_serverless]
// [START cloudrun_service_to_service_auth]
// [START run_service_to_service_auth]
// [START functions_bearer_token]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const url = 'https://TARGET_URL';
// let targetAudience = null;
// Example: https://my-cloud-run-service.run.app/books/delete/12345
// const url = 'https://TARGET_HOSTNAME/TARGET_URL';

// Example: https://my-cloud-run-service.run.app/
// const targetAudience = 'https://TARGET_HOSTNAME/';
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth();

async function request() {
if (!targetAudience) {
// Use the request URL hostname as the target audience for requests.
const {URL} = require('url');
targetAudience = new URL(url);
}
console.info(`request ${url} with target audience ${targetAudience}`);
const client = await auth.getIdTokenClient(targetAudience);
const res = await client.request({url});
Expand Down

0 comments on commit 5ed9105

Please sign in to comment.