Have ideas to improve npm?Join in the discussion! »

    @google-cloud/channel
    TypeScript icon, indicating that this package has built-in type declarations

    1.2.0 • Public • Published

    Google Cloud Platform logo

    Cloud Channel API: Node.js Client

    release level npm version codecov

    Channel client for Node.js

    A comprehensive list of changes in each version may be found in the CHANGELOG.

    Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.

    Table of contents:

    Quickstart

    Before you begin

    1. Select or create a Cloud Platform project.
    2. Enable billing for your project.
    3. Enable the Cloud Channel API API.
    4. Set up authentication with a service account so you can access the API from your local workstation.

    Installing the client library

    npm install @google-cloud/channel

    Using the client library

    // Reads the secrets from a `oauth2.keys.json` file, which should be downloaded
    // from the Google Developers Console and saved in the same directory with the
    // sample app.
    
    // This sample app only calls read-only methods from the Channel API. Include
    // additional scopes if calling methods that modify the configuration.
    const SCOPES = ['https://www.googleapis.com/auth/apps.order'];
    
    async function listCustomers(authClient, accountNumber) {
      // Imports the Google Cloud client library
      const {CloudChannelServiceClient} = require('@google-cloud/channel');
    
      // Instantiates a client using OAuth2 credentials.
      const sslCreds = grpc.credentials.createSsl();
      const credentials = grpc.credentials.combineChannelCredentials(
        sslCreds,
        grpc.credentials.createFromGoogleCredential(authClient)
      );
    
      // Instantiates a client
      const client = new CloudChannelServiceClient({
        sslCreds: credentials,
      });
    
      // Calls listCustomers() method
      const customers = await client.listCustomers({
        parent: `accounts/${accountNumber}`,
      });
      console.info(customers);
    }
    
    /**
     * Create a new OAuth2Client, and go through the OAuth2 content
     * workflow.  Return the full client to the callback.
     */
    function getAuthenticatedClient(keys) {
      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,
          // The first redirect URL from the `oauth2.keys.json` file will be used
          // to generate the OAuth2 callback URL. Update the line below or edit
          // the redirect URL in the Google Developers Console if needed.
          // This sample app expects the callback URL to be
          // 'http://localhost:3000/oauth2callback'
          keys.web.redirect_uris[0]
        );
    
        // Generate the url that will be used for the consent dialog.
        const authorizeUrl = oAuth2Client.generateAuthUrl({
          access_type: 'offline',
          scope: SCOPES.join(' '),
        });
    
        // Open an http server to accept the oauth callback. In this example, the
        // only request to our webserver is to /oauth2callback?code=<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.
            // This line will not work if you are running the code in the
            // environment where a browser is not available. In this case,
            // copy the URL and open it manually in a browser.
            console.info(`Opening the browser with URL: ${authorizeUrl}`);
            open(authorizeUrl, {wait: false}).then(cp => cp.unref());
          });
        destroyer(server);
      });
    }
    
    async function main(accountNumber, keys) {
      // TODO: uncomment with your account number
      // const accountNumber = '1234'
    
      // TODO: uncomment this line with your oAuth2 file
      //const keys = require('./oauth2.keys.json');
    
      getAuthenticatedClient(keys).then(authClient =>
        listCustomers(authClient, accountNumber)
      );
    }

    Samples

    Samples are in the samples/ directory. Each sample's README.md has instructions for running its sample.

    Sample Source Code Try it
    Quickstart source code Open in Cloud Shell

    The Cloud Channel API Node.js Client API Reference documentation also contains samples.

    Supported Node.js Versions

    Our client libraries follow the Node.js release schedule. Libraries are compatible with all current active and maintenance versions of Node.js.

    Client libraries targeting some end-of-life versions of Node.js are available, and can be installed via npm dist-tags. The dist-tags follow the naming convention legacy-(version).

    Legacy Node.js versions are supported as a best effort:

    • Legacy versions will not be tested in continuous integration.
    • Some security patches may not be able to be backported.
    • Dependencies will not be kept up-to-date, and features will not be backported.

    Legacy tags available

    • legacy-8: install client libraries from this dist-tag for versions compatible with Node.js 8.

    Versioning

    This library follows Semantic Versioning.

    This library is considered to be General Availability (GA). This means it is stable; the code surface will not change in backwards-incompatible ways unless absolutely necessary (e.g. because of critical security issues) or with an extensive deprecation period. Issues and requests against GA libraries are addressed with the highest priority.

    More Information: Google Cloud Platform Launch Stages

    Contributing

    Contributions welcome! See the Contributing Guide.

    Please note that this README.md, the samples/README.md, and a variety of configuration files in this repository (including .nycrc and tsconfig.json) are generated from a central template. To edit one of these files, make an edit to its template in this directory.

    License

    Apache Version 2.0

    See LICENSE

    Install

    npm i @google-cloud/channel

    DownloadsWeekly Downloads

    5

    Version

    1.2.0

    License

    Apache-2.0

    Unpacked Size

    3.96 MB

    Total Files

    26

    Last publish

    Collaborators

    • avatar
    • avatar
    • avatar
    • avatar