Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 12 additions & 66 deletions adminSDK/directory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,99 +13,45 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable camelcase */

// [START admin_sdk_directory_quickstart]
import fs from 'fs/promises';
import path from 'path';
import process from 'process';

import path from 'node:path';
import process from 'node:process';
import {authenticate} from '@google-cloud/local-auth';
import {google} from 'googleapis';

// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/admin.directory.user'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = path.join(process.cwd(), 'token.json');
const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json');

/**
* Reads previously authorized credentials from the save file.
*
* @return {Promise<OAuth2Client|null>}
*/
async function loadSavedCredentialsIfExist() {
try {
const content = await fs.readFile(TOKEN_PATH);
const credentials = JSON.parse(content);
return google.auth.fromJSON(credentials);
} catch (err) {
return null;
}
}

/**
* Serializes credentials to a file compatible with GoogleAuth.fromJSON.
*
* @param {OAuth2Client} client
* @return {Promise<void>}
*/
async function saveCredentials(client) {
const content = await fs.readFile(CREDENTIALS_PATH);
const keys = JSON.parse(content);
const key = keys.installed || keys.web;
const payload = JSON.stringify({
type: 'authorized_user',
client_id: key.client_id,
client_secret: key.client_secret,
refresh_token: client.credentials.refresh_token,
});
await fs.writeFile(TOKEN_PATH, payload);
}

/**
* Load or request or authorization to call APIs.
*
* Lists the first 10 users in the domain.
*/
async function authorize() {
let client = await loadSavedCredentialsIfExist();
if (client) {
return client;
}
client = await authenticate({
async function listUsers() {
const auth = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
if (client.credentials) {
await saveCredentials(client);
}
return client;
}

/**
* Lists the first 10 users in the domain.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
async function listUsers(auth) {
const service = google.admin({version: 'directory_v1', auth});
const res = await service.users.list({
const result = await service.users.list({
customer: 'my_customer',
maxResults: 10,
orderBy: 'email',
});

const users = res.data.users;
const users = result.data.users;
if (!users || users.length === 0) {
console.log('No users found.');
return;
}

console.log('Users:');
users.forEach((user) => {
console.log(`${user.primaryEmail} (${user.name.fullName})`);
console.log(`${user.primaryEmail} (${user.name?.fullName})`);
});
}

authorize().then(listUsers).catch(console.error);
await listUsers();

// [END admin_sdk_directory_quickstart]
1 change: 1 addition & 0 deletions adminSDK/directory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"googleapis": "catalog:"
},
"devDependencies": {
"google-auth-library": "catalog:",
"typescript": "catalog:"
},
"engines": {
Expand Down
5 changes: 3 additions & 2 deletions adminSDK/directory/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"extends": "../../tsconfig.base.json"
}
"extends": "../../tsconfig.base.json",
"include": ["**/*.js"]
}
78 changes: 11 additions & 67 deletions adminSDK/reports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,88 +13,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable camelcase */

// [START admin_sdk_reports_quickstart]
import fs from 'fs/promises';
import path from 'path';
import process from 'process';

import path from 'node:path';
import process from 'node:process';
import {authenticate} from '@google-cloud/local-auth';
import {google} from 'googleapis';

// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/admin.reports.audit.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = path.join(process.cwd(), 'token.json');
const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json');

/**
* Reads previously authorized credentials from the save file.
*
* @return {Promise<OAuth2Client|null>}
*/
async function loadSavedCredentialsIfExist() {
try {
const content = await fs.readFile(TOKEN_PATH);
const credentials = JSON.parse(content);
return google.auth.fromJSON(credentials);
} catch (err) {
return null;
}
}

/**
* Serializes credentials to a file compatible with GoogleAuth.fromJSON.
*
* @param {OAuth2Client} client
* @return {Promise<void>}
*/
async function saveCredentials(client) {
const content = await fs.readFile(CREDENTIALS_PATH);
const keys = JSON.parse(content);
const key = keys.installed || keys.web;
const payload = JSON.stringify({
type: 'authorized_user',
client_id: key.client_id,
client_secret: key.client_secret,
refresh_token: client.credentials.refresh_token,
});
await fs.writeFile(TOKEN_PATH, payload);
}

/**
* Load or request or authorization to call APIs.
*
* Lists the last 10 login events for the domain.
*/
async function authorize() {
let client = await loadSavedCredentialsIfExist();
if (client) {
return client;
}
client = await authenticate({
async function listLoginEvents() {
const auth = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
if (client.credentials) {
await saveCredentials(client);
}
return client;
}

/**
* Lists the last 10 login events for the domain.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
async function listLoginEvents(auth) {
const service = google.admin({version: 'reports_v1', auth});
const res = await service.activities.list({
const result = await service.activities.list({
userKey: 'all',
applicationName: 'login',
maxResults: 10,
});
const activities = res.data.items;
const activities = result.data.items;
if (!activities || activities.length === 0) {
console.log('No logins found.');
return;
Expand All @@ -103,10 +47,10 @@ async function listLoginEvents(auth) {
console.log('Logins:');
activities.forEach((activity) => {
console.log(
`${activity.id.time}: ${activity.actor.email} (${activity.events[0].name})`,
`${activity.id?.time}: ${activity.actor?.email} (${activity.events?.[0]?.name})`,
);
});
}

authorize().then(listLoginEvents).catch(console.error);
await listLoginEvents();
// [END admin_sdk_reports_quickstart]
1 change: 1 addition & 0 deletions adminSDK/reports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"googleapis": "catalog:"
},
"devDependencies": {
"google-auth-library": "catalog:",
"typescript": "catalog:"
},
"engines": {
Expand Down
5 changes: 3 additions & 2 deletions adminSDK/reports/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"extends": "../../tsconfig.base.json"
}
"extends": "../../tsconfig.base.json",
"include": ["**/*.js"]
}
79 changes: 13 additions & 66 deletions adminSDK/reseller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,95 +13,42 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable camelcase */

// [START admin_sdk_reseller_quickstart]
import fs from 'fs/promises';
import path from 'path';
import process from 'process';

import path from 'node:path';
import process from 'node:process';
import {authenticate} from '@google-cloud/local-auth';
import {google} from 'googleapis';

// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/apps.order'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = path.join(process.cwd(), 'token.json');
const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json');

/**
* Reads previously authorized credentials from the save file.
*
* @return {Promise<OAuth2Client|null>}
*/
async function loadSavedCredentialsIfExist() {
try {
const content = await fs.readFile(TOKEN_PATH);
const credentials = JSON.parse(content);
return google.auth.fromJSON(credentials);
} catch (err) {
return null;
}
}

/**
* Serializes credentials to a file compatible with GoogleAuth.fromJSON.
*
* @param {OAuth2Client} client
* @return {Promise<void>}
*/
async function saveCredentials(client) {
const content = await fs.readFile(CREDENTIALS_PATH);
const keys = JSON.parse(content);
const key = keys.installed || keys.web;
const payload = JSON.stringify({
type: 'authorized_user',
client_id: key.client_id,
client_secret: key.client_secret,
refresh_token: client.credentials.refresh_token,
});
await fs.writeFile(TOKEN_PATH, payload);
}

/**
* Load or request or authorization to call APIs.
*
* Lists the first 10 subscriptions you manage.
*/
async function authorize() {
let client = await loadSavedCredentialsIfExist();
if (client) {
return client;
}
client = await authenticate({
async function listSubscriptions() {
const auth = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
if (client.credentials) {
await saveCredentials(client);
}
return client;
}

/**
* Lists the first 10 subscriptions you manage.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
async function listSubscriptions(auth) {
const service = google.reseller({version: 'v1', auth});
const res = await service.subscriptions.list({
const result = await service.subscriptions.list({
maxResults: 10,
});
const subscriptions = res.data.subscriptions;
const subscriptions = result.data.subscriptions;
if (!subscriptions || subscriptions.length === 0) {
console.log('No subscriptions found.');
return;
}

console.log('Subscriptions:');
subscriptions.forEach(({customerId, skuId, plan}) => {
console.log(`${customerId} (${skuId}, ${plan.planName})`);
console.log(`${customerId} (${skuId}, ${plan?.planName})`);
});
}
authorize().then(listSubscriptions).catch(console.error);

await listSubscriptions();

// [END admin_sdk_reseller_quickstart]
1 change: 1 addition & 0 deletions adminSDK/reseller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"googleapis": "catalog:"
},
"devDependencies": {
"google-auth-library": "catalog:",
"typescript": "catalog:"
},
"engines": {
Expand Down
5 changes: 3 additions & 2 deletions adminSDK/reseller/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"extends": "../../tsconfig.base.json"
}
"extends": "../../tsconfig.base.json",
"include": ["**/*.js"]
}
Loading
Loading