How to test cloud functions locally with same authorizations as remote

Viewed 124

I've been trying out cloud functions for a little while now. Recently, I found out about functions-framework. It basically allows you to run your functions locally. This helps/should help in reducing the time it takes to test your code. I am running into an issue where calling the functions locally - curl localhost:8080 outputs Insufficient Permission: Request had insufficient authentication scopes while testing them via the console produces the expected result.

I am trying to move data from google drive to google cloud storage

const { google } = require("googleapis");

const { google } = require("googleapis");

const SCOPES = [
  "profile",
  "email",
  "https://www.googleapis.com/auth/drive",
  "https://www.googleapis.com/auth/drive.file",
  "https://www.googleapis.com/auth/drive.readonly",
  "https://www.googleapis.com/auth/drive.metadata.readonly",
  "https://www.googleapis.com/auth/drive.appdata",
  "https://www.googleapis.com/auth/drive.metadata",
];

async function addDocument(authClient, bucket, data) {
  const version = "v1";
  const storage = google.storage({ version, auth: authClient });

  const response = await storage.objects.insert({
    bucket,
    requestBody: {
      name: data.name,
    },
    media: {
      body: data.content,
      mimeType: data.mimeType,
    },
  });

  return response.data;
}

async function getDocumentContent(authClient, fileId) {
  const version = "v3";

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

  const json = await drive.files.get({
    alt: "media",
    fileId: fileId,
    auth: authClient,
  });

  const content = await drive.files.get({
    alt: "media",
    fileId: fileId,
    auth: authClient,
  });

  const data = {
    content: content.data,
    mimeType: json.data.mimeType,
    name: json.data.name,
  };

  return data;
}

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 = "folder_id";
  const bucketName = "some_bucket";

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

};

My question here is, how do I get local calls to have the same authentication as my remote calls?

1 Answers

I wrote an article on that. At the time where I wrote the article, only Java and Go (because I contributed to implement the feature in the client library) were compliant.

Have a try with NodeJS, it might be implemented now.

Related