0

so im currently developing a software which hosts your vps automaticly and bypasses the gcloud free tier 50 hours limit. so anyways on the login part, it executes the command "gcloud auth login --no-launch-browser" and sends the link to a discord channel, but then, it requires a code, ive already setted up a message listner which executes after 7 seconds of sending the gcloud auth link.

so yeah how do I input the code, any help would be apreciated

heres my full code

const Discord = require("discord.js")
const { Client, Intents } = require('discord.js');
const { token, channelId } = require("./config.json");
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
var embed = new Discord.MessageEmbed()
client.on("ready", () => {
  console.log(`(login bot) Logged in as ${client.user.tag}!`)
})
const { spawn } = require( 'child_process' );
 
 
client.on("message", msg => {
      if (msg.content.startsWith("WARNING: The --[no-]launch-browser flags are deprecated and will be removed in future updates. Use --no-browser to replace --no-launch-browser.")){
        msg.delete();
      }        
  
 
 
 
  if(msg.content === '!ping2'){
    embed = embed
    .setDescription(`pong!2`)
    .setColor("BLUE")
    return msg.channel.send({embeds: [embed]})
  }
 
  if(msg.content.toLowerCase() === '!login'){
    
 
        //collector
             //collectors const
             let filter = (m) => m.author.id === msg.author.id
             let limit = 60000 * 15 //minute
             const collected = {filter, time: limit, max: 1, errors: ['time']};
     
             //collectors const
             //aray
             var colArray = {}
     
     
     
     
                         //collectorThing
             const collector = async function() {
                 let genderEmbed = await msg.channel.send('Please enter in the code that you recived') 
             msg.channel
             .awaitMessages(collected)
             .then((collected) => {
 
                 let code = collected.first()
               colArray.code = code  
              const codeCollected = spawn( 'echo', [ colArray.code ] );
 
              codeCollected.stdout.on( 'data', ( data ) => {
                msg.channel.send(data.toString())
            } )
 
              codeCollected.stderr.on( 'data', ( data ) => {
                  msg.channel.send(data.toString())
              } )
              codeCollected.on( 'out', ( code ) => {
                msg.channel.send(code.toString())
            } )
          
             }
             )
             .catch(collected => {
                 console.log(collected)
             })
         }
 
 //collectorEnd
 
 
 
    'use strict';
    const login = async function() {
 
    const login = spawn( 'gcloud', [ 'auth', 'login', '--no-launch-browser' ] );
        
    login.stderr.on( 'data', ( data ) => {
        msg.channel.send(data.toString())
    } )
   
  }
 
  login()
  setTimeout(collector, 7000);
 
  }
 
}
)
 
client.login(token)

edit: also heres what it does currently heres what it does currently(image)

Leuqas
  • 19
  • 1
  • 8

1 Answers1

0

As mentioned in the thread :

To authenticate a fresh gcloud when GOOGLE_APPLICATION_CREDENTIALS points to a file with user credentials rather than service account credentials.

cat ${GOOGLE_APPLICATION_CREDENTIALS} 
{
 "client_id": "aaa",
 "client_secret": "bbb",
 "refresh_token": "ccc",
 "type": "authorized_user"
 }
 gcloud config set auth/client_id aaa 
gcloud config set auth/client_secret bbb 
cloud auth activate-refresh-token user ccc
 

This uses the undocumented auth activate-refresh-token subcommand - which isn't ideal - but it does work. Paired with gcloud auth activate-service-account --key-file=credentials.json, this makes it possible to initialize gcloud regardless of the credential type available at $GOOGLE_APPLICATION_CREDENTIALS.

You can also refer to the thread :

For automated processes, a service account is the recommended way. You can use the google-oauth library for this. You can generate an access token like this

# With default credential (your user account or the Google Cloud Component service account.
 # Or with the service account key file defined in the GOOGLE_APPLICATION_CREDENTIALS env var -> for platform outside GCP) 
credentials, project_id = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
 # With service account key file (not recommended) 
# credentials = service_account.Credentials.from_service_account_file('service-account.json', 
# scopes=["https://www.googleapis.com/auth/cloud-platform"]) 
from google.auth.transport import requests 
credentials.refresh(requests.Request()) 
print(credentials.token)

For more information, you can refer to the thread where local development without using a service account has been discussed.

Divyani Yadav
  • 1,030
  • 4
  • 9
  • well as i said i dont really wanna use cred files as i want this project to be accesible even to those who dont know much abt these type of stuffs to i want it to be simple as posible thats why im using discord for it, it would be the same process as loging in on your terminal by "gcloud auth login --no-browser" but instead of your browser, the output is on discord. – Leuqas Apr 20 '22 at 14:13
  • im using discord because the people that will be hosting it will host it on heroku, and heroku dosent really allow terminal/console inputs – Leuqas Apr 20 '22 at 14:17