Node.js 服务器中 Stripe API 的身份验证错误:缺少 API 密钥

问题描述 投票:0回答:1

我正在开发一个订阅服务,在客户端使用 React,在服务器端使用 Node.js。但是,我在尝试与 Stripe API 交互时遇到身份验证错误。错误消息指出我没有提供 API 密钥,尽管我希望我的设置能够正确处理此问题。以下是我的客户的相关部分。有人遇到过类似的问题吗

用 React Native 编写客户端代码

const subscribe = async () => {
    try {
      const response = await fetch(
        "https://expressjs-production-0dbf.up.railway.app/create-checkout-session",
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
        },
      );

      const data = await response.json();

      if (!response.ok) {
        return Alert.alert(
          "Error RESPONSE",
          data.message ||
            "There was a problem creating the payment session",
        );
      }

      // Redirect the user to the Checkout payment session
      const { url, session } = data;
      Linking.openURL(url);

      update(ref(db, `/users/${auth.currentUser.uid}/user`), {
        sessionId: session.id,
      });

      setModal(null);
    } catch (error) {
      console.log(error);
      Alert.alert(
        "Error",
        "An error occurred while trying to subscribe",
      );
    }
  };

通常情况下,stripe 会打开一个会话,以便用户可以访问付款,但我收到 400 错误,如下所示

`{"error": {"message": "You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY'). See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/."}}

react-native api stripe-payments
1个回答
0
投票

您只分享了您的客户端代码。该错误似乎是由

/create-checkout-session
路线上的服务器端代码引发的。您应该检查您是否正确初始化了 Stripe 服务器端 SDK。

您可以参考:https://docs.stripe.com/api/authentication

© www.soinside.com 2019 - 2024. All rights reserved.