我已成功部署 Google Cloud Function。它可以在浏览器中工作,但是当我尝试使用 React Native Firebase 从我的 React Native 项目中调用它时,我收到控制台警告:“可能未处理的承诺拒绝(id:0):错误:未经身份验证”。我的功能代码是:
// functions/index.js
const functions = require('firebase-functions');
const {OpenAI} = require('openai');
async function determineBrandFromModelNumber(modelNumber) {
const openai = new OpenAI({apiKey: OPENAI_API_KEY});
const completion = await openai.chat.completions.create({
model: 'gpt-4',
// eslint-disable-next-line max-len
messages: [{role: 'user', content: `Determine the brand of the tool with model number: ${modelNumber}`}],
});
console.log(completion.choices[0].message.content);
return extractBrandInfo(completion.choices[0].message.content.toLowerCase());
}
function extractBrandInfo(text) {
console.log('TEXT: ', text);
switch (true) {
case (text.includes('dewalt')):
console.log('HIT');
return 'dewalt';
case (text.includes('stanley')):
return 'stanley';
}
}
exports.determineBrand = functions.https.onCall((data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('unauthenticated', 'Endpoint requires authentication!');
} else {
return determineBrandFromModelNumber(data.modelNumber);
}
});
我在这里称之为:
const handleSavePress = async () => {
const { data } = await firebase.functions().httpsCallable('determineBrand')(
{
modelNumber: modelNo,
},
);
console.log('DATA: ', data);
const tool: Tool = {
tid: generateUUID(15),
modelNum: modelNo ? modelNo : '',
serialNum: serialNo ? serialNo : '',
name: toolName,
imageUris: photos ? photos : [],
owner: firebase.auth().currentUser.uid,
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
};
console.log('TOOL: ', tool);
handleCreateTool(tool);
navigation.goBack();
};
当我尝试调用handleSavePress()方法时,我收到未经身份验证的警告,但没有错误,但firebase.functions()调用后没有任何console.log语句被执行,handleCreateTool(tool)或导航也没有执行。 goBack(),因此它在 firebase.functions() 调用中的某处挂起。 function/index.js 中的 console.log 语句也没有被执行。
我已在权限中将所有AuthenticatedUsers 设置为云函数调用者。我还尝试根据此处的另一个问题将其设置为 Firebase Viewer,但这也不起作用。
我是 Google Cloud Functions 的新手,如有任何帮助,我们将不胜感激。
我通过将 allUsers 添加到 Cloud Functions Invoker 权限来修复此问题。