Error: You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments

Error: You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments

我正在尝试使用以下命令设置 google 应用程序凭据 set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH(我将 KEY_PATH 替换为包含我的服务的 JSON 文件的路径帐户密钥)。

当我运行这个命令

gcloud auth application-default print-access-token一切正常。

但是我在浏览器上收到这个错误

Error: {"servicePath":"vision.googleapis.com","port":443,"clientConfig":{},"fallback":true}You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments. Use OAuth2Client from google-auth-library.

这是我使用vision的文件:

import React, { useEffect, useRef } from "react";
import Webcam from "react-webcam";
      
      const Home = () => {
          // Get a reference to the Cloud Vision API component
const Vision = require('@google-cloud/vision');
const vision = new Vision.ImageAnnotatorClient();

        const webcamRef = React.useRef(null);
        const [imgSrc, setImgSrc] = React.useState(null);
      
        const capture = React.useCallback(() => {
          const imageSrc = webcamRef.current.getScreenshot();

          let text;
    vision.textDetection(imageSrc)
        .then(([detections]) => {
            const annotation = detections.textAnnotations[0];
            text = annotation ? annotation.description : '';
            console.log(`Extracted text: ${text}`);
            console.log(`Extracted text from image (${text.length} chars)`);
        }).catch(vis_err => {
            console.error("Vision error:" , vis_err);
        });
          setImgSrc(imageSrc);
        }, [webcamRef, setImgSrc]);
      
        return (
            <>
              <Webcam
                audio={false}
                ref={webcamRef}
                screenshotFormat="image/jpeg"
              />
              <button onClick={capture}>Capture photo</button>
              {imgSrc && (
                <img
                  src={imgSrc}
                />
              )}
            </>
          );
        };
export default Home

谁能解释为什么会发生这种情况以及如何解决?如有任何帮助,我们将不胜感激!

此问题已于 Github 关闭:

this library is supposed to be used from a server-side Node.js application, not from any front-end environment such as a browser, Electron app, React, (name your front-end framework here). If you just run the code by plain regular Node.js, it will work.Having said that - we do have experimental support for a browser use case starting from the latest version, which is 0.11.0. It's experimental (just implemented) and not really documented yet. You can try using it though. To do that, you need to pass an authenticated instance of OAuth2Client (from google-auth-library) as an auth parameter of the client constructor

这里是link:https://github.com/googleapis/nodejs-dialogflow/issues/405#issuecomment-529713296