2

im trying to implement documentai with NodeJS

and im stuck while trying to run DocumentProcessorServiceClient()

i got this error

    Error: 7 PERMISSION_DENIED: Permission 'documentai.processors.processOnline' denied on resource '//documentai.googleapis.com/projects/<project-id>/locations/us/processors/<processor-id>' (or it may not exist).
      at Object.callErrorFromStatus (C:\Users\dev\me-ocr\node_modules\@grpc\grpc-js\build\src\call.js:31:26)
      at Object.onReceiveStatus (C:\Users\dev\me-ocr\node_modules\@grpc\grpc-js\build\src\client.js:176:52)
      at Object.onReceiveStatus (C:\Users\dev\me-ocr\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:336:141)
      at Object.onReceiveStatus (C:\Users\dev\me-ocr\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:299:181)
      at C:\Users\dev\me-ocr\node_modules\@grpc\grpc-js\build\src\call-stream.js:130:78 
      at processTicksAndRejections (internal/process/task_queues.js:79:11) {
    code: 7,
    details: "Permission 'documentai.processors.processOnline' denied on resource '//documentai.googleapis.com/projects/<project-id>/locations/us/processors/<processor-id>' (or it may not exist).",
    metadata: Metadata { internalRepr: [Map], options: {} },
    note: 'Exception occurred in retry method that was not classified as transient'
  }

here is my function

const doScan = async (filePath: string) => {
  try {
    // Configure the request for processing the PDF
    const name = `projects/${projectId}/locations/${location}/processors/${processorId}`; 
    // Read the file into memory.
    const imageFile = await fs.promises.readFile(filePath); 
    // Convert the image data to a Buffer and base64 encode it.
    const encodedImage = Buffer.from(imageFile).toString("base64"); 
    const request = {
      name,
      document: {
        content: encodedImage,
        mimeType: "application/pdf",
      },
    };   
    // Recognizes text entities in the PDF document
    const [result] = await client.processDocument(request);  
    const { document }: any = result;
    const { text }: any = document;
    const getText = (textAnchor: any) => {
      if (!textAnchor.textSegments || textAnchor.textSegments.length === 0) {
        return "";
      } 
      // First shard in document doesn't have startIndex property
      const startIndex = textAnchor.textSegments[0].startIndex || 0;
      const endIndex = textAnchor.textSegments[0].endIndex; 
      return text.substring(startIndex, endIndex);
    }; 
    // Read the text recognition output from the processor
    console.log("The document contains the following paragraphs:");
    const [page1] = document.pages;
    const { paragraphs } = page1;

    for (const paragraph of paragraphs) {
      const paragraphText = getText(paragraph.layout.textAnchor);
      console.log(`Paragraph text:\n${paragraphText}`);
    }
  } catch (err) {
    console.log({ err });
  }
};

i've already try change the name using this, but it ends the same... client.processorPath(projectId, location, processorId);

Thanks

aemon4
  • 1,037
  • 6
  • 11
  • Have you followed [this](https://cloud.google.com/document-ai/docs/before-you-begin) guide to authenticate? Meaning, are you using a service account and the environment variable `GOOGLE_APPLICATION_CREDENTIALS`? Or are you passing the service account credentials inside the code when initializing the client? Remember that you need to authenticate the calls to avoid the permission denied errors. – aemon4 Feb 23 '21 at 09:51
  • yeah im passing the credetial inside the code const client = new DocumentProcessorServiceClient({ apiEndpoint: `${location}-documentai.googleapis.com`, keyFilename: "GCPKey.json", }); – CheapImpact Apr 22 '21 at 07:34
  • btw the case is solved now after the v2 release, im somehow able to do it by upgrading the version of the package – CheapImpact Apr 22 '21 at 07:36

1 Answers1

0

According to comment from the poster, the issue was resolved by upgrading to the latest version of the Document AI Node.JS Package

Holt Skinner
  • 1,692
  • 1
  • 8
  • 21