首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用与远程相同的授权在本地测试云功能

如何使用与远程相同的授权在本地测试云功能
EN

Stack Overflow用户
提问于 2022-03-31 15:08:32
回答 1查看 173关注 0票数 1

我尝试云功能已经有一段时间了。最近,我发现了函数框架。它基本上允许您在本地运行您的函数。这有助于/应该有助于减少测试代码所需的时间。我遇到了一个问题,在调用本地函数时,curl localhost:8080输出Insufficient Permission: Request had insufficient authentication scopes,同时通过控制台进行测试,生成预期的结果。

我正在尝试将数据从google驱动器转移到google云存储。

代码语言:javascript
运行
复制
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);
  }

};

我在这里的问题是,如何让本地呼叫具有与远程呼叫相同的身份验证?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71694966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档