django-allauth重定向URL说“登录失败”与Stripe OAuth

yhuiod9q  于 2023-10-21  发布在  Go
关注(0)|答案(2)|浏览(116)

使用django-allauth通过Stripe Connect验证用户。似乎连接到条纹,并允许登录,但当返回到我的网站,它呈现了allauth错误模板,其中说:

An error occurred while attempting to login via your social network account.

你觉得我哪里做错了吗?
我是这么做的。
settings.py

INSTALLED_APPS = [
                  ...
                  'allauth',
                  'allauth.account',
                  'allauth.socialaccount',
                  'allauth.socialaccount.providers.stripe',
                   ...
                  ]

AUTHENTICATION_BACKENDS = [
                           ...
                           'allauth.account.auth_backends.AuthenticationBackend',
                           ...
                           ]

SITE_ID = 1

urls.py

urlpatterns = [
               ...
               url(r'^accounts/', include('allauth.urls')),
               ...
              ]

login.html

{% if user and not.is_anonymous %}
    <h2>Hello, {{ user.get_full_name }}.</h2>

{% else %}
    <p><a href="{% provider_login_url 'stripe' %}" class="btn btn-default">Stripe</a></p>

{% endif %}

Stripe Connect

Website URL: https://127.0.0.1:8000

Redirect_URIs: http://127.0.0.1:8000/accounts/stripe/login/callback/

在django admin中设置social应用程序,如下所示:

Social Applications = http://127.0.0.1:8000/
uxhixvfz

uxhixvfz1#

oops.我在allauth管理社交应用程序字段中使用Client_ID而不是Stripe Connect Jmeter 板中的API密钥。

kxe2p93d

kxe2p93d2#

我也有同样的问题。我的解决方案:

template_name = "orders/order-create.html"
    form_class = OrderForm
    success_url = reverse_lazy("orders:order-create")

    def post(
        self, request: HttpRequest, *args: tuple, **kwargs: dict
    ) -> HttpResponseRedirect:
        form = self.get_form()
        if form.is_valid():
            super().post(request, *args, **kwargs)
            baskets = Basket.objects.filter(user=self.request.user)
            checkout_session = stripe.checkout.Session.create(
                line_items=baskets.stripe_products(),
                metadata={
                    "order_id": self.object.id,
                    "user_id": self.request.user.id,  # save user
                },
                mode="payment",
                success_url="{}{}".format(
                    settings.DOMAIN_NAME, reverse("orders:order-success")
                )
                + "?session_id={CHECKOUT_SESSION_ID}",
                cancel_url="{}{}".format(
                    settings.DOMAIN_NAME, reverse("orders:order-canceled")
                ),
            )
            request.session["checkout_session_id"] = checkout_session.id
            return HttpResponseRedirect(
                checkout_session.url, status=HTTPStatus.SEE_OTHER
            )
        else:
            return render(request, self.template_name, {"form": form})

class SuccessTemplateView(TemplateView):
    template_name = "orders/success.html"

    def get(self, request, *args, **kwargs) -> HttpResponse:
        session_id = request.GET.get("session_id")

        if session_id:
            # Retrieve information from Stripe session metadata
            session = stripe.checkout.Session.retrieve(session_id)
            user_id = session.metadata.get("user_id")

            if user_id:
                try:
                    user = get_user_model().objects.get(id=user_id)
                    user.backend = (
                       "allauth.account.auth_backends.AuthenticationBackend"
                    )
                    login(request, user)
                except get_user_model().DoesNotExist:
                    return HttpResponseBadRequest(
                        "User does not exist", status=HTTPStatus.BAD_REQUEST
                    )
        return super().get(request, *args, **kwargs)

相关问题