使用 onPost 在 Blazor 服务器应用程序上处理 Spotify 的 POST 回调给出“糟糕!糟糕!出了点问题”

问题描述 投票:0回答:0

我正在尝试开发 Blazor 服务器端应用程序。在我的一个 Blazor 组件中,我有一个按钮,单击该按钮会向 Spotify 的授权端点发送 HTTP GET 请求。我通过使用

NavigationManager.NavigateTo(requestEndpoint.ToString());
做到这一点 这部分的行为符合预期,调用 Spotify 端点,用户被重定向到登录 Spotify,然后显示一个对话框,要求授权我的应用程序能够看到一些用户信息。

但是,根据 Spotify API 文档,在下一步中,我应该接收到我在 GET 请求中指定的 URI 端点的回调,带有两个参数。为了处理这个回调,(我假设这是对我的应用程序的 POST 请求,我尝试使用 Razor 页面,因为这是我在网上找到的关于如何使用 Blazor 服务器应用程序接受 POST 请求的最被接受的方法。这就是我有:

在 SpotifyLogin.cshtml.cs 中

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Pluto.Pages;

[IgnoreAntiforgeryToken]
public class SpotifyLoginPost : PageModel
{
    public bool code{ get; set; }
    public string state{ get; set; }

    public void OnPost()
    {
        Console.WriteLine("heya!");
    }
}

在 SpotifyLogin.cshtml 中

@page
@model Pluto.Pages.SpotifyLoginPost
@attribute [IgnoreAntiforgeryToken]


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>Error</title>
    <link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
    <link href="~/css/site.css" rel="stylesheet" asp-append-version="true" />
</head>

<body>
    <div class="main">
        <div class="content px-4">
            <h1 class="text-danger">Error.</h1>
            <h2 class="text-danger">An error occurred while processing your request.</h2>

            @if (Model.code)
            {
                <p>
                    <strong>Hey!!</strong> <code>@Model.code</code>
                </p>
            }
        </div>
    </div>
</body>

</html>

cshtml 文件有我在网上找到的伪代码,因为到目前为止,我并没有真正尝试实现任何东西,只是想看看 POST 请求是否会自动运行

onPost
方法,但我不认为确实如此。在 PostMan 中,当我向我的
localhost:<PORTNUMBER>/SpotifyLogin
发送一个虚拟的 POST 请求时,它的工作方式与呈现正确的 HTML 一样。但是,在我的应用程序上,一旦重定向到 Spotify 结束,它只会说“糟糕,出了点问题!”。我在这里做错了什么?我的应用程序可以处理 POST 请求,那么为什么它不能处理 Spotify 的任何回调?我已确保在我的 Spotify 应用程序门户上设置了正确的重定向 URL。

有人可以帮我弄清楚如何让回调工作吗?

asp.net post blazor spotify
© www.soinside.com 2019 - 2024. All rights reserved.