Daniel Lozada
Daniel Lozada

Reputation: 65

AUTHENTICATION PROBLEM WITH STRIPE JS PAYMENT GATEWAY

I am from Colombia and I registered in Stripe putting the United States as the country since Colombia is not on that list.

Then I tried to authenticate as the documentation indicates and it turns out that it always throws me the following error:

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/."
type: "invalid_request_error"
}

The way I am trying is as follows:

import STRIPE_KEYS from "./stripe-keys.js";
//console.log(STRIPE_KEYS);

const d = document,
  $donation = d.getElementById("donation"),
  $template = d.getElementById("donation-template") /* .content */,
  $fragment = d.createDocumentFragment(),
  fetchOptions = {
    headers: {
      Authorization: `Bearer${STRIPE_KEYS.secret}`,
    },
  };

let products, prices;
Promise.all([
  fetch("https://api.stripe.com/v1/products", fetchOptions),
  fetch("https://api.stripe.com/v1/prices", fetchOptions),
])
  .then((responses) => Promise.all(responses.map((res) => res.json())))
  .then((json) => {
    console.log(json);
  })
  .catch((err) => {
    console.log(err);
    let message = err.statusText || "ERROR trying to connect to STRIPE API";
    $donation.innerHTML = `<p><b>Error ${err.status}: ${message}</b></p>`;
  });

However, and although STRIPE_KEYS.secret brings the secret key that I need to provide in the "Authorization" header, it still gives me the same 401 error. Is there any person from Colombia who has been successful in authenticating with this STRIPE API?

enter image description here

Upvotes: 2

Views: 694

Answers (1)

floatingLomas
floatingLomas

Reputation: 8737

As mentioned in the comments, you are missing a space between BEARER and your key - but you also likely should be using Stripe Node instead of writing from scratch here: https://github.com/stripe/stripe-node

Upvotes: 2

Related Questions