Google 云功能曾经完美运行。然后他们停止工作并开始将我重定向到登录页面?

Google Cloud Functions Used to Work Perfectly. Then They Stopped Working and Started Redirecting Me to A Login Page?

我已经使用 Google Cloud Functions 一个多星期了,它们非常棒。我使用一个简单的 python 3.9 函数在我的 Next.js 应用程序中将字符串打印到我的终端(用于测试目的)并且它运行良好。这是我的示例 Google 云函数。

def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
    request_json = request.get_json()
    if request.args and 'message' in request.args:
        return request.args.get('message')
    elif request_json and 'message' in request_json:
        return request_json['message']
    else:
        return f'Function ran'

这是我的 Next.js 调用函数的代码:

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next"
import { GoogleAuth } from "google-auth-library"

export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
    const url = process.env.FUNCTION_URL as string

    //Example with the key file, not recommended on GCP environment.
    const auth = new GoogleAuth({ keyFilename: process.env.KEYSTORE_PATH })

    //Create your client with an Identity token.
    const client = await auth.getIdTokenClient(url)
    const result = await client.request({ url })
    console.log(result.data)
    res.json({ data: result.data })
}

我写了另一个函数来做同样的事情,现在每个函数都只将原始 html 打印到控制台?当我打开 index.html 文件中的文本时,它看起来像这样。

我完全重写了原来的云函数,即使这样也不起作用了。它将相同的 html 打印到控制台。到底是怎么回事?我的代码是完全一样的,现在它坏了...?

这是一种简单的“大海捞针”类型的修复。我从云函数列表视图中获取函数 url,单击所需函数上的“操作菜单”下拉菜单,然后选择“复制函数”。这将我带到配置显示页面,它在“触发器”部分中具有功能 url。

注意我用红色圈出的内容。出于某种原因,此页面将 -1 附加到函数 url。这就是让我失败的原因。在我的代码中,我从存储在 process.env.FUNCTION_URL 变量中的 url 中删除了 -1。这使它成功了!