【问题标题】:Stripe connect integration with DjangoStripe Connect 与 Django 的集成
【发布时间】:2021-01-03 19:44:02
【问题描述】:

我是 django 中的新集成条带,但最近,我遇到了一个错误,我找不到答案,每次我尝试将条带帐户连接到我的网站时,都会出现此错误:

{"error":{"message":"没有应用程序匹配提供的客户端标识符"}}

代码如下:

import urllib

import requests

from django.urls import reverse
from django.http import HttpResponseRedirect
from django.views import View
from django.conf import settings
from django.shortcuts import redirect

from .models import Seller


class StripeAuthorizeView(View):

    def get(self, request):
        if not self.request.user.is_authenticated:
            return HttpResponseRedirect(reverse('login'))
        url = 'https://connect.stripe.com/oauth/authorize'
        params = {
            'response_type': 'code',
            'scope': 'read_write',
            'client_id': settings.STRIPE_CONNECT_CLIENT_ID,
            'redirect_uri': f'http://localhost:8000/users/oauth/callback'
        }
        url = f'{url}?{urllib.parse.urlencode(params)}'
        return redirect(url)


class StripeAuthorizeCallbackView(View):

    def get(self, request):
        code = request.GET.get('code')
        if code:
            data = {
                'client_secret': settings.STRIPE_SECRET_KEY,
                'grant_type': 'authorization_code',
                'client_id': settings.STRIPE_CONNECT_CLIENT_ID,
                'code': code
            }
            url = 'https://connect.stripe.com/oauth/token'
            resp = requests.post(url, params=data)
            # add stripe info to the seller
            stripe_user_id = resp.json()['stripe_user_id']
            stripe_access_token = resp.json()['access_token']
            seller = Seller.objects.filter(user_id=self.request.user.id).first()
            seller.stripe_access_token = stripe_access_token
            seller.stripe_user_id = stripe_user_id
            seller.save()
        url = reverse('home')
        response = redirect(url)
        return response

settings.py

STRIPE_PUBLISHABLE_KEY = '<your test publishable key here>'
STRIPE_SECRET_KEY = '<your test secret key here>'
STRIPE_CONNECT_CLIENT_ID = '<your test connect client id here>'

【问题讨论】:

    标签: python django stripe-payments payment environment


    【解决方案1】:

    您似乎在应该使用 data=data 时使用 params=data:

           data = {
                'client_secret': settings.STRIPE_SECRET_KEY,
                'grant_type': 'authorization_code',
                'client_id': settings.STRIPE_CONNECT_CLIENT_ID,
                'code': code
            }
            url = 'https://connect.stripe.com/oauth/token'
            #resp = requests.post(url, params=data)
            resp = requests.post(url, data=data) #Try with this instead
    

    从请求文档中,参数用于 Url 参数: 您经常希望在 URL 的查询字符串 中发送某种数据。如果您手动构建 URL,则此数据将作为 URL 中问号后的键/值对给出,例如httpbin.org/get?key=val。 Requests 允许您将这些参数作为字符串字典提供,使用 params 关键字参数

    https://requests.readthedocs.io/en/master/user/quickstart/

    对于 data=data 部分,也来自同一页面: Requests 的简单 API 意味着所有形式的 HTTP 请求都是显而易见的。例如,这是您发出 HTTP POST 请求的方式

    >>> r = requests.post('https://httpbin.org/post', data = {'key':'value'})
    

    【讨论】:

    • 嘿伙计,谢谢你的回答,但最后一部分我不太明白
    • 哪一部分?你需要做的是使用 resp = requests.post(url, data=data) 而不是 resp = requests.post(url, params=data)
    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2017-08-12
    • 2015-02-07
    • 1970-01-01
    相关资源
    最近更新 更多