【问题标题】:Unhandled error { Error: You did not provide an API key未处理的错误 { 错误:您没有提供 API 密钥
【发布时间】:2020-12-10 16:11:03
【问题描述】:

我正在将 Stripe 与我的 iOS 项目集成到 Firebase 中,这是我在尝试打开 PaymentOptionsViewController(在我的 iOS 应用模拟器中)时收到的错误:

未处理的错误 { 错误:您没有提供 API 密钥。您需要使用 Bearer auth 在 Authorization 标头中提供您的 API 密钥(例如“授权:Bearer YOUR_SECRET_KEY”)。详情请参阅https://stripe.com/docs/api#authentication,或者我们可以通过https://support.stripe.com/ 提供帮助。

这是我的 nodejs index.js 中的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

const stripe = require("stripe")(functions.config().stripe.secret_test_key);

exports.createStripeCustomer = functions.firestore.document('users/{userId}').onCreate(async (snap, context) => {
const data = snap.data();
const email = data.email;

const customer = await stripe.customers.create({ email: email })
return admin.firestore().collection('users').doc(data.id).update({ stripeId : customer.id})
});

exports.createCharge = functions.https.onCall(async (data, context) => {

const customerId = data.customerId;
const totalAmount = data.total;
const idempotency = data.idempotency;
const uid = context.auth.uid

if (uid === null) {
    console.log('Illegal access attempt due to unauthenticated user');
    throw new functions.https.HttpsError('permission-denied', 'Illegal access attempt.')
}

return stripe.charges.create({
    amount: totalAmount,
    currency: 'usd',
    customer: customerId
}, {
    idempotency_key: idempotency
}).then( _ => {
    return
}).catch( err => {
    console.log(err);
    throw new functions.https.HttpsError('internal', 'Unable to create charge')
});
})

exports.createEphemeralKey = functions.https.onCall(async (data, context) => {

const customerId = data.customer_id;
const stripeVersion = data.stripe_version;
const uid = context.auth.uid;

if (uid === null) {
    console.log('Illegal access attempt due to unauthenticated user');
    throw new functions.https.HttpsError('permission-denied', 'Illegal access attempt.')
}

let key = await stripe.ephemeralKeys.create(
  {customer: '{{CUSTOMER_ID}}'},
  {stripe_version: '{{2019-05-16}}'}
);


return stripe.ephemeralKeys.create(
    {customer: customerId},
    {stripe_version: stripeVersion}
).then((key) => {
    return key
}).catch((err) => {
    console.log(err)
    throw new functions.https.HttpsError('internal', 'Unable to create ephemeral key.')
})
})



// exports.helloWorld = functions.https.onRequest((request, response) => {
//     console.log('This is the console message.')
//  response.send("Hello from JonnyB Codes!");
// });s

如何解决上述错误?如果需要更多信息来获得答案,请告诉我。

【问题讨论】:

  • 你能解决这个问题吗?

标签: node.js firebase google-cloud-functions


【解决方案1】:

在 iOS 中也收到此错误消息。 就我而言,在启动应用程序时,在付款之前我没有正确设置可发布密钥。在 Swift 中,这成功了。

Stripe.setDefaultPublishableKey("pk_test_....")

可能不能解决你的问题,但也许会给你一个线索。

【讨论】:

    猜你喜欢
    • 2021-08-27
    • 2017-01-18
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 2019-03-29
    • 2014-07-13
    • 1970-01-01
    相关资源
    最近更新 更多