const getClient = options => { return new google.auth.OAuth2( options.client_id, options.client_secret, options.redirect_url ); }
const genAuthUrl = (options, stateUrl) => { const client = getClient(options); const opts = { access_type: 'offline', scope: AUTH_SCOPES, state: stateUrl, }; if (isV2AuthApi(google.auth.OAuth2.GOOGLE_OAUTH2_AUTH_BASE_URL_)) { opts.prompt = 'consent'; } else { opts.approval_prompt = 'force'; } return client.generateAuthUrl(opts); }
async authorize(credentials) { const {client_secret, client_id, redirect_uris} = credentials.installed const oAuth2Client = new google.auth.OAuth2( client_id, client_secret, redirect_uris[0]) // Check if we have previously stored a token. try { let token = await fs.readFile(this.token_path) oAuth2Client.setCredentials(JSON.parse(token)) } catch (err) { let res = await this.getAccessToken(oAuth2Client) return res } return oAuth2Client }
function getAuthenticationUrl() { var client = new google.auth.OAuth2( config.oauth2.clientId, config.oauth2.clientSecret, config.oauth2.redirectUrl ); // Use 'profile' scope to authorize fetching the user's profile return client.generateAuthUrl({ scope: ['profile'] }); }
function getAccessToken() { return new Promise(function(resolve, reject) { var key = serviceAccount var jwtClient = new google.auth.JWT( key.client_email, null, key.private_key, SCOPES, null ) jwtClient.authorize(function(err, tokens) { if (err) { reject(err) return } resolve(tokens.access_token) }) }) }
function main( projectId = process.env.GCLOUD_PROJECT, cloudRegion = 'us-central1' ) { // [START healthcare_list_datasets] const {google} = require('googleapis'); const healthcare = google.healthcare('v1beta1'); async function listDatasets() { const auth = await google.auth.getClient({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }); google.options({auth}); // TODO(developer): uncomment these lines before running the sample // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; const parent = `projects/${projectId}/locations/${cloudRegion}`; const request = {parent}; const dataset = await healthcare.projects.locations.datasets.list(request); console.log(dataset.data); } listDatasets(); // [END healthcare_list_datasets] }
const createAuthCredential = async () => { try { let authClient = await google.auth.getApplicationDefault(); authClient = authClient.credential; if (authClient.createScopedRequired && authClient.createScopedRequired()) { authClient = authClient.createScoped([ 'https://www.googleapis.com/auth/jobs', ]); } return google.jobs({ version: 'v3', auth: authClient, }); } catch (e) { return console.error(e); } }
// [END healthcare_patch_hl7v2_store] // Returns an authorized API client by discovering the Healthcare API with // the provided API key. // [START healthcare_get_client] function getClient(apiKey, serviceAccountJson, cb) { const API_VERSION = 'v1alpha'; const DISCOVERY_API = 'https://healthcare.googleapis.com/$discovery/rest'; google.auth .getClient({scopes: ['https://www.googleapis.com/auth/cloud-platform']}) .then(authClient => { const discoveryUrl = `${DISCOVERY_API}?labels=CHC_ALPHA&version=${API_VERSION}&key=${apiKey}`; google.options({auth: authClient}); google .discoverAPI(discoveryUrl) .then(client => { cb(client); }) .catch(err => { console.error(err); }); }); }
// Returns an authorized API client by discovering the Cloud IoT Core API with // the provided API key. const getClient = async () => { const authClient = await google.auth.getClient({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }); const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}`; google.options({ auth: authClient, }); try { return google.discoverAPI(discoveryUrl); } catch (err) { console.log('Error during API discovery.', err); } }
function getUser(authorizationCode, callback) { var client = new google.auth.OAuth2( config.oauth2.clientId, config.oauth2.clientSecret, config.oauth2.redirectUrl ); // With the code returned from OAuth flow, get an access token client.getToken(authorizationCode, function(err, tokens) { if (err) return callback(err); // Configure this Google API client to use the access token client.setCredentials(tokens); // Call the Google+ API to get the profile of the user who authenticated google.plus('v1').people.get({ userId: 'me', auth: client }, function(err, profile) { if (err) return callback(err); var user = { id: profile.data.id, name: profile.data.displayName, imageUrl: profile.data.image.url }; callback(null, user); }); }); }
// [END setup] // [START auth] function auth(callback) { google.auth.getApplicationDefault((err, authClient) => { if (err) { return callback(err); } // The createScopedRequired method returns true when running on GAE or a // local developer machine. In that case, the desired scopes must be passed // in manually. When the code is running in GCE or GAE Flexible, the scopes // are pulled from the GCE metadata server. // See https://cloud.google.com/compute/docs/authentication for more // information. if (authClient.createScopedRequired && authClient.createScopedRequired()) { // Scopes can be specified either as an array or as a single, // space-delimited string. authClient = authClient.createScoped([ 'https://www.googleapis.com/auth/cloud-platform', ]); } callback(null, authClient); }); }
// Returns an authorized API client by discovering the Cloud IoT Core API with // the provided API key. const getClient = async serviceAccountJson => { // the getClient method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS // environment variables if serviceAccountJson is not passed in const authClient = await google.auth.getClient({ keyFilename: serviceAccountJson, scopes: ['https://www.googleapis.com/auth/cloud-platform'], }); const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}`; google.options({ auth: authClient, }); try { return google.discoverAPI(discoveryUrl); } catch (err) { console.log('Error during API discovery.', err); } }
// [END healthcare_patch_hl7v2_message] // Returns an authorized API client by discovering the Healthcare API with // the provided API key. // [START healthcare_get_client] function getClient(apiKey, serviceAccountJson, cb) { const API_VERSION = 'v1alpha'; const DISCOVERY_API = 'https://healthcare.googleapis.com/$discovery/rest'; google.auth .getClient({scopes: ['https://www.googleapis.com/auth/cloud-platform']}) .then(authClient => { const discoveryUrl = `${DISCOVERY_API}?labels=CHC_ALPHA&version=${API_VERSION}&key=${apiKey}`; google.options({auth: authClient}); google .discoverAPI(discoveryUrl) .then(client => { cb(client); }) .catch(err => { console.error(err); }); }); }
// [END healthcare_get_fhir_store_metadata] // Returns an authorized API client by discovering the Healthcare API with // the provided API key. // [START healthcare_get_client] function getClient(apiKey, serviceAccountJson, cb) { const API_VERSION = 'v1alpha'; const DISCOVERY_API = 'https://healthcare.googleapis.com/$discovery/rest'; google.auth .getClient({scopes: ['https://www.googleapis.com/auth/cloud-platform']}) .then(authClient => { const discoveryUrl = `${DISCOVERY_API}?labels=CHC_ALPHA&version=${API_VERSION}&key=${apiKey}`; google.options({auth: authClient}); google .discoverAPI(discoveryUrl) .then(client => { cb(client); }) .catch(err => { console.error(err); }); }); }
// [END healthcare_export_dicom_instance_gcs] // Returns an authorized API client by discovering the Healthcare API with // the provided API key. // [START healthcare_get_client] function getClient(apiKey, serviceAccountJson, cb) { const API_VERSION = 'v1alpha'; const DISCOVERY_API = 'https://healthcare.googleapis.com/$discovery/rest'; google.auth .getClient({scopes: ['https://www.googleapis.com/auth/cloud-platform']}) .then(authClient => { const discoveryUrl = `${DISCOVERY_API}?labels=CHC_ALPHA&version=${API_VERSION}&key=${apiKey}`; google.options({auth: authClient}); google .discoverAPI(discoveryUrl) .then(client => { cb(client); }) .catch(err => { console.error(err); }); }); }