首页 > 解决方案 > 未处理的错误 { 错误:您没有提供 API 密钥

问题描述

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

未处理的错误 { 错误:您没有提供 API 密钥。您需要在 Authorization 标头中提供您的 API 密钥,使用 Bearer auth(例如“Authorization: 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.jsfirebasegoogle-cloud-functions

解决方案


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

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

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


推荐阅读