1 year ago

#364828

test-img

prismo

Why is scope not getting added to access token?

I run gcloud auth application-default login to allow my code to access my GCP resources locally with functions-framework. It seems to work fine when calling functions from @google-cloud/whatever but when I try to instantiate a drive object from the googleapis library, I get: Insufficient Permission: Request had insufficient authentication scopes..

This is a snippet of what my code looks like:

const SCOPES = ["https://www.googleapis.com/auth/drive"];

async function getDocuments(authClient, folderId) {
  const query = `parents= '${folderId}'`;

  const fields = "files(id, name)";

  const version = "v3";

  const drive = google.drive({ version, client: authClient });

  const response = await drive.files.list({
    q: query,
    fields,
    auth: authClient,
  });

  const { files } = response.data;
  return files;
}

exports.copy = async (req, res) => {
   const auth = new google.auth.GoogleAuth({
    scopes: SCOPES,
   });

   const client = await auth.getClient();

   const containingFolder = "some_id";

   try{
     const files = await getDocuments(client, containingFolder)
    res.send(files)
   }catch(err){
    res.send(err)
   }
}




I checked my access token via https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=token and under scopes, this is what I see:

"scope": "openid https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/accounts.reauth",

I do not see the scope I provided here

   const auth = new google.auth.GoogleAuth({
    scopes: SCOPES,
   });

Any idea how I can fix this?

FD: This code works when it's ran on GCP. I just want to be able to test locally but it's proving mighty difficult.

node.js

google-cloud-platform

google-cloud-iam

0 Answers

Your Answer

Accepted video resources