AUTH FAIL When calling Firebase Function with Gemini AI/Embedding/Sentiment score

1 day ago 7
ARTICLE AD BOX

This is the error im getting in console, in flutter console when testing, i get UNAUTHENTICATED. However, im able to login to the app, and make other calls like getting data from firestore, etc.

In local env everything is fine and works great.

import { GoogleGenAI } from "@google/genai"; const apiKey = process.env.GEMINI_API_KEY; const ai = apiKey ? new GoogleGenAI({ apiKey }) : null;

Here is my full function.

export const createAQuickPin = onCall(async (data, context) => { const { pinContent } = data.data; if (!pinContent) { throw new HttpsError('invalid-argument', 'Missing or invalid parameters.'); } if (!data.auth) { throw new HttpsError('unauthenticated', 'The function must be called while authenticated.'); } const uid = data.auth.uid; // Check for AI client configuration if (!ai) { throw new HttpsError('internal', 'AI service is not configured. Check the server logs for the missing API key error.'); } var systemInstruction = AI_CREATE_PIN; try { systemInstruction = systemInstruction.replace(/\$pinContent/g, pinContent); const geminiResponsePromise = ai.models.generateContent({ model: modelName, contents: pinContent, config: { systemInstruction: systemInstruction } }); const sentimentPromise = textClient.analyzeSentiment({ document: { content: pinContent, type: "PLAIN_TEXT" } }); const embedText = `${pinContent}`; const embeddingModel = "gemini-embedding-001"; const embeddingPromise = ai.models.embedContent({ model: embeddingModel, contents: embedText, }); const [embeddedResponse, sentimentResponse, geminiResponse] = await Promise.all([ embeddingPromise, sentimentPromise, geminiResponsePromise ]); const responseText = geminiResponse.text.trim(); const parsed = JSON.parse(responseText); const embedding = embeddedResponse.embeddings[0].values; const sentimentScore = sentimentResponse[0].sentences[0].sentiment.score; // --- 5. Database Write (only if valid) --- if (parsed.isValid) { const pinRef = db .collection("users") .doc(uid) .collection("pins") .doc(); await pinRef.set({ pinCategory: parsed.pinCategory, pinSubject: parsed.pinSubject, pinContent: pinContent, pinConfidence: parsed.confidence, seekAdvice: parsed.seekAdvice, embedding: embedding, sentimentScore: sentimentScore, pinAddedDate: FieldValue.serverTimestamp(), }); } return { promptFromUser: pinContent, pinCategory: parsed.pinCategory, pinSubject: parsed.pinSubject, confidence: parsed.confidence, seekAdvice: parsed.seekAdvice, isValid: parsed.isValid, modelUsed: modelName }; } catch (error) { console.error('Error in createAQuickPin:', error); throw new HttpsError('unavailable', 'The AI service is temporarily unavailable or returned an error.'); } });

textPayload: "The request was not authorized to invoke this service. Read more at https://cloud.google.com/run/docs/securing/authenticating Additional troubleshooting documentation can be found at: https://cloud.google.com/run/docs/troubleshooting#401"

here is my IAM enter image description here

ive asked gemini and followed it and still nothing... any help would be AMAZING!

Read Entire Article