Flutter调用可调用云函数失败,错误为[firebase_functions/permission-denied] PERMISSION_DENIED

zfycwa2u  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(155)

当我试图从Flutter调用一个可调用函数时,我遇到了这个错误:

W/FirebaseContextProvider( 3655): Error getting App Check token. Error: com.google.firebase.FirebaseException: Error returned from API. code: 403 body: App attestation failed.
D/TrafficStats( 3655): tagSocket(294) with statsTag=0xffffffff, statsUid=-1
E/flutter ( 3655): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_functions/permission-denied] PERMISSION_DENIED
E/flutter ( 3655):
E/flutter ( 3655): #0      StandardMethodCodec.decodeEnvelope
message_codecs.dart:653
E/flutter ( 3655): #1      MethodChannel._invokeMethod
platform_channel.dart:315
E/flutter ( 3655): <asynchronous suspension>
E/flutter ( 3655): #2      MethodChannelHttpsCallable.call
method_channel_https_callable.dart:22
E/flutter ( 3655): <asynchronous suspension>
E/flutter ( 3655): #3      HttpsCallable.call
https_callable.dart:49
E/flutter ( 3655): <asynchronous suspension>
E/flutter ( 3655): #4      _SendFeedbackModalBottomSheetState.build.<anonymous closure>
send_feedback.dart:90
E/flutter ( 3655): <asynchronous suspension>
E/flutter ( 3655):
E/flutter ( 3655): #0      StandardMethodCodec.decodeEnvelope
message_codecs.dart:653
E/flutter ( 3655): #1      MethodChannel._invokeMethod
platform_channel.dart:315
E/flutter ( 3655): <asynchronous suspension>
E/flutter ( 3655): #2      MethodChannelHttpsCallable.call
method_channel_https_callable.dart:22
E/flutter ( 3655): <asynchronous suspension>
E/flutter ( 3655): #3      HttpsCallable.call
https_callable.dart:49
E/flutter ( 3655): <asynchronous suspension>
E/flutter ( 3655): #4      _SendFeedbackModalBottomSheetState.build.<anonymous closure>
send_feedback.dart:90
E/flutter ( 3655): <asynchronous suspension>
E/flutter ( 3655):

下面是调用可调用函数的方法:

final HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('sendFeedback');
                  final resp = await callable.call(<String, dynamic>{
                    'email': 'customer@example.com',
                    'message': message,
                  });

以下是云函数的外观:

const sendgridApiKey = functions.config().sendgrid.key;
sgMail.setApiKey(sendgridApiKey);
export const sendFeedback = functions.https.onCall(async (data, context) => {
  // Check if the user is authenticated
  if (!context.auth) {
    throw new functions.https.HttpsError("unauthenticated", "User is not authenticated");
  }

  const message = data.message;

  try {
    // Compose the email message
    const emailMessage = {
      to: "hello@example.com",
      from: "no-reply@example.com",
      subject: "Feedback from user",
      text: message,
    };

    // Send the email using SendGrid
    await sgMail.send(emailMessage);

    return {
      success: true, message: "Email sent successfully",
    };
  } catch (error) {
    console.error("Error sending email:", error);
    throw new functions.https.HttpsError("internal", "An error occurred while sending the email");
  }
});

这种情况发生在模拟器和真实设备上。我已经检查了服务帐户“App Engine默认服务帐户”,它的角色是“编辑器”。我需要做什么来修复权限错误?
更新:我今天尝试按照www.example.com中的指示使用App Check调试令牌,但没有看到错误“错误获取App Check令牌。https://firebase.google.com/docs/app-check/flutter/debug-provider#androidAPI返回错误。代码:403主体:应用程序证明失败了。但是拒绝许可的错误还是发生了。 App attestation failed" anymore. But the permission denied error still happened.

e3bfsja2

e3bfsja21#

最后,我可以通过将角色Cloud Functions InvokerallUsers主体添加到函数中来解决这个错误。

相关问题