How to use where clause in Knex
Create refunds with Stripe
Load HTML with Cheerio
How to send POST request with Axios
Find intersection of array in Lodash
Powered by Official white Bloop logo with a period

Terms / Privacy / Search / Support

  • const { GoogleAuth } = require("google-auth-library");
    
    /**
     * Instead of specifying the type of client you'd like to use (JWT, OAuth2, etc)
     * this library will automatically choose the right client based on the environment.
     */
    async function main() {
      const auth = new GoogleAuth({
        scopes:
          "https://www.googleapis.com/auth/cloud-platform",
      });
      const client = await auth.getClient();
      const projectId = await auth.getProjectId();
      const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
      const res = await client.request({ url });
      console.log(res.data);
    }
    
    main().catch(console.error);
    
    Docs
    0
  • async function main() {
      const auth = new GoogleAuth({
        scopes:
          "https://www.googleapis.com/auth/cloud-platform",
      });
      const client = await auth.getClient();
      const projectId = await auth.getProjectId();
      // List all buckets in a project.
      const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
      const res = await client.request({ url });
      console.log(res.data);
    }
    
    Docs
    0
  • const {
      GoogleAuth,
      Impersonated,
    } = require("google-auth-library");
    const {
      SecretManagerServiceClient,
    } = require("@google-cloud/secret-manager");
    
    async function main() {
      // Acquire source credentials:
      const auth = new GoogleAuth();
      const client = await auth.getClient();
    
      // Impersonate new credentials:
      let targetClient = new Impersonated({
        sourceClient: client,
        targetPrincipal:
          "impersonated-account@projectID.iam.gserviceaccount.com",
        lifetime: 30,
        delegates: [],
        targetScopes: [
          "https://www.googleapis.com/auth/cloud-platform",
        ],
      });
    
      // Get impersonated credentials:
      const authHeaders =
        await targetClient.getRequestHeaders();
      // Do something with `authHeaders.Authorization`.
    
      // Use impersonated credentials:
      const url =
        "https://www.googleapis.com/storage/v1/b?project=anotherProjectID";
      const resp = await targetClient.request({ url });
      for (const bucket of resp.data.items) {
        console.log(bucket.name);
      }
    
      // Use impersonated credentials with google-cloud client library
      // Note: this works only with certain cloud client libraries utilizing gRPC
      //    e.g., SecretManager, KMS, AIPlatform
      // will not currently work with libraries using REST, e.g., Storage, Compute
      const smClient = new SecretManagerServiceClient({
        projectId: anotherProjectID,
        auth: {
          getClient: () => targetClient,
        },
      });
      const secretName =
        "projects/anotherProjectNumber/secrets/someProjectName/versions/1";
      const [accessResponse] =
        await smClient.accessSecretVersion({
          name: secretName,
        });
    
      const responsePayload =
        accessResponse.payload.data.toString("utf8");
      // Do something with the secret contained in `responsePayload`.
    }
    
    main();
    
    Docs
    0
  • const { GoogleAuth } = require("google-auth-library");
    
    /**
     * Instead of specifying the type of client you'd like to use (JWT, OAuth2, etc)
     * this library will automatically choose the right client based on the environment.
     */
    async function main() {
      const auth = new GoogleAuth({
        scopes:
          "https://www.googleapis.com/auth/cloud-platform",
      });
      const client = await auth.getClient();
      const projectId = await auth.getProjectId();
      const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
      const res = await client.request({ url });
      console.log(res.data);
    }
    
    main().catch(console.error);
    
    Docs
    0
  • async function main() {
      const auth = new GoogleAuth({
        scopes:
          "https://www.googleapis.com/auth/cloud-platform",
      });
      const client = await auth.getClient();
      const projectId = await auth.getProjectId();
      // List all buckets in a project.
      const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
      const res = await client.request({ url });
      console.log(res.data);
    }
    
    Docs
    0
  • const client = await auth.getClient();
    
    client.on("tokens", (tokens) => {
      if (tokens.refresh_token) {
        // store the refresh_token in my database!
        console.log(tokens.refresh_token);
      }
      console.log(tokens.access_token);
    });
    
    const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
    const res = await client.request({ url });
    // The `tokens` event would now be raised if this was the first request
    
    Docs
    0
  • const { OAuth2Client } = require("google-auth-library");
    const http = require("http");
    const url = require("url");
    const open = require("open");
    const destroyer = require("server-destroy");
    
    // Download your OAuth2 configuration from the Google
    const keys = require("./oauth2.keys.json");
    
    /**
     * Start by acquiring a pre-authenticated oAuth2 client.
     */
    async function main() {
      const oAuth2Client = await getAuthenticatedClient();
      // Make a simple request to the People API using our pre-authenticated client. The `request()` method
      // takes an GaxiosOptions object.  Visit https://github.com/JustinBeckwith/gaxios.
      const url =
        "https://people.googleapis.com/v1/people/me?personFields=names";
      const res = await oAuth2Client.request({ url });
      console.log(res.data);
    
      // After acquiring an access_token, you may want to check on the audience, expiration,
      // or original scopes requested.  You can do that with the `getTokenInfo` method.
      const tokenInfo = await oAuth2Client.getTokenInfo(
        oAuth2Client.credentials.access_token
      );
      console.log(tokenInfo);
    }
    
    /**
     * Create a new OAuth2Client, and go through the OAuth2 content
     * workflow.  Return the full client to the callback.
     */
    function getAuthenticatedClient() {
      return new Promise((resolve, reject) => {
        // create an oAuth client to authorize the API call.  Secrets are kept in a `keys.json` file,
        // which should be downloaded from the Google Developers Console.
        const oAuth2Client = new OAuth2Client(
          keys.web.client_id,
          keys.web.client_secret,
          keys.web.redirect_uris[0]
        );
    
        // Generate the url that will be used for the consent dialog.
        const authorizeUrl = oAuth2Client.generateAuthUrl({
          access_type: "offline",
          scope:
            "https://www.googleapis.com/auth/userinfo.profile",
        });
    
        // Open an http server to accept the oauth callback. In this simple example, the
        // only request to our webserver is to /oauth2callback?code=
        const server = http
          .createServer(async (req, res) => {
            try {
              if (req.url.indexOf("/oauth2callback") > -1) {
                // acquire the code from the querystring, and close the web server.
                const qs = new url.URL(
                  req.url,
                  "http://localhost:3000"
                ).searchParams;
                const code = qs.get("code");
                console.log(`Code is ${code}`);
                res.end(
                  "Authentication successful! Please return to the console."
                );
                server.destroy();
    
                // Now that we have the code, use that to acquire tokens.
                const r = await oAuth2Client.getToken(code);
                // Make sure to set the credentials on the OAuth2 client.
                oAuth2Client.setCredentials(r.tokens);
                console.info("Tokens acquired.");
                resolve(oAuth2Client);
              }
            } catch (e) {
              reject(e);
            }
          })
          .listen(3000, () => {
            // open the browser to the authorize url to start the workflow
            open(authorizeUrl, { wait: false }).then((cp) =>
              cp.unref()
            );
          });
        destroyer(server);
      });
    }
    
    main().catch(console.error);
    
    Docs
    0
  • const {
      ExternalAccountClient,
    } = require("google-auth-library");
    const jsonConfig = require("/path/to/config.json");
    
    async function main() {
      const client = ExternalAccountClient.fromJSON(jsonConfig);
      client.scopes = [
        "https://www.googleapis.com/auth/cloud-platform",
      ];
      // List all buckets in a project.
      const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
      const res = await client.request({ url });
      console.log(res.data);
    }
    
    Docs
    0
  • const {
      GoogleAuth,
      Impersonated,
    } = require("google-auth-library");
    const {
      SecretManagerServiceClient,
    } = require("@google-cloud/secret-manager");
    
    async function main() {
      // Acquire source credentials:
      const auth = new GoogleAuth();
      const client = await auth.getClient();
    
      // Impersonate new credentials:
      let targetClient = new Impersonated({
        sourceClient: client,
        targetPrincipal:
          "impersonated-account@projectID.iam.gserviceaccount.com",
        lifetime: 30,
        delegates: [],
        targetScopes: [
          "https://www.googleapis.com/auth/cloud-platform",
        ],
      });
    
      // Get impersonated credentials:
      const authHeaders =
        await targetClient.getRequestHeaders();
      // Do something with `authHeaders.Authorization`.
    
      // Use impersonated credentials:
      const url =
        "https://www.googleapis.com/storage/v1/b?project=anotherProjectID";
      const resp = await targetClient.request({ url });
      for (const bucket of resp.data.items) {
        console.log(bucket.name);
      }
    
      // Use impersonated credentials with google-cloud client library
      // Note: this works only with certain cloud client libraries utilizing gRPC
      //    e.g., SecretManager, KMS, AIPlatform
      // will not currently work with libraries using REST, e.g., Storage, Compute
      const smClient = new SecretManagerServiceClient({
        projectId: anotherProjectID,
        auth: {
          getClient: () => targetClient,
        },
      });
      const secretName =
        "projects/anotherProjectNumber/secrets/someProjectName/versions/1";
      const [accessResponse] =
        await smClient.accessSecretVersion({
          name: secretName,
        });
    
      const responsePayload =
        accessResponse.payload.data.toString("utf8");
      // Do something with the secret contained in `responsePayload`.
    }
    
    main();
    
    Docs
    0
  • const { GoogleAuth } = require("google-auth-library");
    
    /**
     * Instead of specifying the type of client you'd like to use (JWT, OAuth2, etc)
     * this library will automatically choose the right client based on the environment.
     */
    async function main() {
      const auth = new GoogleAuth({
        scopes:
          "https://www.googleapis.com/auth/cloud-platform",
      });
      const client = await auth.getClient();
      const projectId = await auth.getProjectId();
      const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
      const res = await client.request({ url });
      console.log(res.data);
    }
    
    main().catch(console.error);
    
    Docs
    0
  • async function main() {
      const auth = new GoogleAuth({
        scopes:
          "https://www.googleapis.com/auth/cloud-platform",
      });
      const client = await auth.getClient();
      const projectId = await auth.getProjectId();
      // List all buckets in a project.
      const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
      const res = await client.request({ url });
      console.log(res.data);
    }
    
    Docs
    0
  • const client = await auth.getClient();
    
    client.on("tokens", (tokens) => {
      if (tokens.refresh_token) {
        // store the refresh_token in my database!
        console.log(tokens.refresh_token);
      }
      console.log(tokens.access_token);
    });
    
    const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
    const res = await client.request({ url });
    // The `tokens` event would now be raised if this was the first request
    
    Docs
    0
  • const { OAuth2Client } = require("google-auth-library");
    const http = require("http");
    const url = require("url");
    const open = require("open");
    const destroyer = require("server-destroy");
    
    // Download your OAuth2 configuration from the Google
    const keys = require("./oauth2.keys.json");
    
    /**
     * Start by acquiring a pre-authenticated oAuth2 client.
     */
    async function main() {
      const oAuth2Client = await getAuthenticatedClient();
      // Make a simple request to the People API using our pre-authenticated client. The `request()` method
      // takes an GaxiosOptions object.  Visit https://github.com/JustinBeckwith/gaxios.
      const url =
        "https://people.googleapis.com/v1/people/me?personFields=names";
      const res = await oAuth2Client.request({ url });
      console.log(res.data);
    
      // After acquiring an access_token, you may want to check on the audience, expiration,
      // or original scopes requested.  You can do that with the `getTokenInfo` method.
      const tokenInfo = await oAuth2Client.getTokenInfo(
        oAuth2Client.credentials.access_token
      );
      console.log(tokenInfo);
    }
    
    /**
     * Create a new OAuth2Client, and go through the OAuth2 content
     * workflow.  Return the full client to the callback.
     */
    function getAuthenticatedClient() {
      return new Promise((resolve, reject) => {
        // create an oAuth client to authorize the API call.  Secrets are kept in a `keys.json` file,
        // which should be downloaded from the Google Developers Console.
        const oAuth2Client = new OAuth2Client(
          keys.web.client_id,
          keys.web.client_secret,
          keys.web.redirect_uris[0]
        );
    
        // Generate the url that will be used for the consent dialog.
        const authorizeUrl = oAuth2Client.generateAuthUrl({
          access_type: "offline",
          scope:
            "https://www.googleapis.com/auth/userinfo.profile",
        });
    
        // Open an http server to accept the oauth callback. In this simple example, the
        // only request to our webserver is to /oauth2callback?code=
        const server = http
          .createServer(async (req, res) => {
            try {
              if (req.url.indexOf("/oauth2callback") > -1) {
                // acquire the code from the querystring, and close the web server.
                const qs = new url.URL(
                  req.url,
                  "http://localhost:3000"
                ).searchParams;
                const code = qs.get("code");
                console.log(`Code is ${code}`);
                res.end(
                  "Authentication successful! Please return to the console."
                );
                server.destroy();
    
                // Now that we have the code, use that to acquire tokens.
                const r = await oAuth2Client.getToken(code);
                // Make sure to set the credentials on the OAuth2 client.
                oAuth2Client.setCredentials(r.tokens);
                console.info("Tokens acquired.");
                resolve(oAuth2Client);
              }
            } catch (e) {
              reject(e);
            }
          })
          .listen(3000, () => {
            // open the browser to the authorize url to start the workflow
            open(authorizeUrl, { wait: false }).then((cp) =>
              cp.unref()
            );
          });
        destroyer(server);
      });
    }
    
    main().catch(console.error);
    
    Docs
    0
  • const client = await auth.getClient();
    
    client.on("tokens", (tokens) => {
      if (tokens.refresh_token) {
        // store the refresh_token in my database!
        console.log(tokens.refresh_token);
      }
      console.log(tokens.access_token);
    });
    
    const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
    const res = await client.request({ url });
    // The `tokens` event would now be raised if this was the first request
    
    Docs
    0
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples