Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Authentication is not working for google cloud API authentication with NodeJS

I am trying to download a file from google drive using Google service account. For that I have created a project, created a service account and keys. Now when I am trying to use google auth api, It is returning undefined.

Please find the code below.

const scope = ["https://www.googleapis.com/auth/drive"];
const authService = new google.auth.GoogleAuth({
  keyFile: "./service.json",                                                                                                                                                                                                                                            
  scopes: scope,
});

this is returning something like below,

GoogleAuth {
  checkIsGCE: undefined,
  jsonContent: null,
  cachedCredential: null,
  _cachedProjectId: null,
  keyFilename: './service.json',
  scopes: [ 'https://www.googleapis.com/auth/cloud-platform' ],
  clientOptions: undefined
}

although service.json is in the same folder and copied the credentials from workspace itself.

Please let me know. Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Remember that you need to share the file with the service account so that it will have permissions to download the file.

// service account key file from Google Cloud console.
const KEYFILEPATH = 'C:\\ServiceAccountCred.json';

// Request full drive access.
const SCOPES = ['https://www.googleapis.com/auth/drive'];

// Create a service account initialize with the service account key file and scope needed
const auth = new google.auth.GoogleAuth({
    keyFile: KEYFILEPATH,
    scopes: SCOPES
});

 const driveService = google.drive({version: 'v3', auth});

var fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
var dest = fs.createWriteStream('/tmp/photo.jpg');
driveService.files.get({
  fileId: fileId,
  alt: 'media'
})
    .on('end', function () {
      console.log('Done');
    })
    .on('error', function (err) {
      console.log('Error during download', err);
    })
    .pipe(dest);

To see how authorization works check File upload node.js

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda