Example #1
0
  google: function(req, res) {
    var CLIENT_ID = req.body.clientId,
      CLIENT_SECRET = sails.config.GOOGLE_SECRET,
      REDIRECT_URI = req.body.redirectUri,
      oauth2Client = new google.auth.OAuth2(
        CLIENT_ID,
        CLIENT_SECRET,
        REDIRECT_URI
      );

    google.options({ auth: oauth2Client });

    oauth2Client.getToken(req.body.code, function(err, tokens) {
      if (err) return res.negotiate(err);
      oauth2Client.setCredentials(tokens);

      request(
        "https://www.googleapis.com/userinfo/v2/me",
        {
          auth: { bearer: tokens.access_token }
        },
        function(err, response, body) {
          if (err) return res.negotiate(err);
          const profile = JSON.parse(body);

          var validEmail = isValidEmail(profile);

          if (!validEmail)
            return res.send(
              403,
              "You must be a U.S. Fish & Wildlife Service Employee to create an account."
            );
          // Find or Create a user account
          User.findOne({ email: profile.email }).exec(function(err, foundUser) {
            if (err) return res.negotiate(err);
            if (foundUser) {
              var jwt = sailsTokenAuth.createToken(foundUser);
              return res.send({ token: jwt });
            } else {
              var params = {
                name: profile.name,
                email: profile.email
              };

              User.create(params).exec(function(err, newUser) {
                if (err) return res.negotiate(err);
                var jwt = sailsTokenAuth.createToken(newUser);
                res.send({ token: jwt });
              });
            }
          });
        }
      );
    });
  }
    .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);
        });
    });
const _setAuthCredential = async () => {
  const res = await auth.getApplicationDefault();

  let client = res.credential;
  if (client.hasScopes && !client.hasScopes()) {
    client = client.createScoped([
      'https://www.googleapis.com/auth/cloud-billing',
      'https://www.googleapis.com/auth/cloud-platform',
    ]);
  }

  // Set credential globally for all requests
  google.options({
    auth: client,
  });
};
    .then(authClient => {
      const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}`;

      google.options({
        auth: authClient,
      });

      google
        .discoverAPI(discoveryUrl)
        .then(client => {
          cb(client);
        })
        .catch(err => {
          console.log('Error during API discovery.', err);
        });
    });
  async function createDataset() {
    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 datasetId = 'my-dataset';
    const parent = `projects/${projectId}/locations/${cloudRegion}`;
    const request = {parent, datasetId};

    await healthcare.projects.locations.datasets.create(request);
    console.log(`Created dataset: ${datasetId}`);
  }