Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API

I’m trying create a endpoint for upload a image using Gemini and i want Gemini returts to me some informations about the uploaded image.

Here’s my code:

const { GoogleGenerativeAI } = require("@google/generative-ai");
const { GoogleAIFileManager } = require("@google/generative-ai/server");
const path = require('path');
const dotenv = require("dotenv");

dotenv.config();

const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash"});

const fileManager = new GoogleAIFileManager(process.env.API_KEY);
const filePath = path.join(__dirname, '../assets/jetpack.jpg');

async function uploadImg() {
  const uploadResponse = await fileManager.uploadFile(filePath, {
    mimeType: "image/jpeg",
    displayName: "jetpack drawing",
  });

  console.log(`Uploaded file ${uploadResponse.file.displayName} as: ${uploadResponse.file.uri}`);

  const getResponse = await fileManager.getFile(uploadResponse.file.name);
  console.log(`Retrieved file ${getResponse.displayName} as ${getResponse.uri}`);

  const result = await model.generateContent([
    {
      fileData: {
        mimeType: uploadResponse.file.mimeType,
        fileUri: uploadResponse.file.uri,
      }
    },
    { text: "Describe this image." },
  ]);
  // console.log(result.response.text());
  return result.response.text();
}

uploadImg();
module.exports = uploadImg;

When i run this code with node, i had sucess:
PS C:\Users\victo\Documents\Pasta pessoal\Entrevistas\Teste Shopper\SHOPPER\api\src\services> node geminiService.ts
Uploaded file jetpack drawing as: https://generativelanguage.googleapis.com/v1beta/files/3uzvp9in1lmt
Retrieved file jetpack drawing as https://generativelanguage.googleapis.com/v1beta/files/3uzvp9in1lmt

But, when i try open this api link i recevi this error:

// 20240828104158
// https://generativelanguage.googleapis.com/v1beta/files/3uzvp9in1lmt

{
  "error": {
    "code": 403,
    "message": "Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.",
    "status": "PERMISSION_DENIED"
  }
}

I’m already have the permisson in the console on google cloud:

Here are a rest of my code:

import { Response, Request } from "express";
const uploadImg = require('../services/geminiService');

export const uploadImgController = async (req: Request, res: Response) => {
  try {
    const response = await uploadImg();
    res.json({ message: "File processed successfully", data: response });
  } catch (error: any) {
    res.status(500).json({ message: error.message });
  }
};

I tested this code in postman and i had success:

{
    "message": "File processed successfully",
    "data": "The image is a hand-drawn diagram of a \"Jetpack Backpack\". It is drawn on lined notebook paper in blue ink. The backpack is depicted as a typical backpack with two small rocket boosters on the bottom.  The diagram labels the features of the backpack including the padded strap support, the USB-C charging port, the 15-minute battery life, and the retractable boosters.  It also notes that the backpack fits a 18-inch laptop, looks like a normal backpack, is lightweight, and is steam-powered and green/clean."
}

To be clear - your code works correctly? You can use this URI in a fileData part and get a result?

The reason you can’t access the URI directly is that you need to include an API Key as part of your request. The URI isn’t meant for public use - it is just a reference to the upload that you can give to Gemini.

Most significantly - once you upload the image to the File API, you can’t get the image back from the File API or the URI it provides. This is intentional. You can get metadata about it, but not the data itself.