I was following a YouTube video on building an Video Subscriptions Site in Django. However, while integrating stripe I got this error, “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/.).”
I am beginner so I don’t know much about it, here is the Models.py Code: The code is identical to the video
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name="Membership",
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
('slug', models.SlugField()),
('membership_type', models.CharField(choices=[('Enterprise', 'ent'), ('Professional', 'pro'), ('Free', 'free')], default="Free", max_length=30)),
('price', models.IntegerField(default=15)),
('stripe_plan_id', models.CharField(max_length=40)),
],
),
migrations.CreateModel(
name="UserMembership",
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
('stripe_customer_id', models.CharField(max_length=40)),
('membership', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='membership.membership')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name="Subscription",
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
('stripe_customer_id', models.CharField(max_length=40)),
('user_membership', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='membership.usermembership')),
],
),
]
Urls.py
from django.conf import settings
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Settings.py
"""
Django settings for videosub project.
Generated by 'django-admin startproject' using Django 5.0.
For more information on this file, see
url
For the full list of settings and their values, see
url
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See url
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-1-6st9*c$g_s%$^$t13)tf99iviiy8)pj3vvtwcw$$jr(xfx)_'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#Own
'membership',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'videosub.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'videosub.wsgi.application'
# Database
# url
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# url
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# urls/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# url
STATIC_URL = 'static/'
# Default primary key field type
# url
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
if DEBUG:
STRIPE_PUBLISHABLE_KEY = ''
STRIPE_SECRET_KEY = ''
else:
#live Key
STRIPE_PUBLISHABLE_KEY = ''
STRIPE_SECRET_KEY = ''
There is nothing in Views.py and I was getting this error while accessing /admin/login/, when I am trying to see if models are showing in Django admin dashboard.
I appericate any help…
I was trying to interigate Stripe payments API in Django project but got this error (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 stripe url for details, or we can help at stripe url)
You didn’t share how are you using Stripe API key, I invite you to follow this python quickstart: stripe.com/docs/payments/quickstart?lang=python to understand how you can make API call to Stripe using python.
I see in your code you have: if DEBUG: STRIPE_PUBLISHABLE_KEY = ” STRIPE_SECRET_KEY = ” else: #live Key STRIPE_PUBLISHABLE_KEY = ” STRIPE_SECRET_KEY = ” Did you forget to fill these variables with the corresponding values? stripe.com/docs/keys
Yes, brother, Is I have to put my accounts Pkey and Skey in these blanks?
It worked brother, thank so you much for your help.