【问题标题】:im getting a error 401 unauthorized using the spotify api in c# wpf我在 c# wpf 中使用 spotify api 收到错误 401 未授权
【发布时间】:2019-07-06 13:36:53
【问题描述】:

我收到此错误。 System.Net.WebException:'远程服务器返回错误:(401)未经授权。'代码如下。

当我取出方法“PrintUsefulData(api)”时,一切似乎都正常 该方法有一个 http 客户端 webrequest。我正在尝试请求以下https://api.spotify.com/v1/albums

    static void Main(string[] args)
    {
        _clientId = string.IsNullOrEmpty(_clientId)
            ? Environment.GetEnvironmentVariable("my_clientId")//my id
            : _clientId;

        _secretId = string.IsNullOrEmpty(_secretId)
            ? Environment.GetEnvironmentVariable("my_secretId") // my id
            : _secretId;


        AuthorizationCodeAuth auth =
            new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:5002", "http://localhost:5002", Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
        auth.AuthReceived += AuthOnAuthReceived;
        auth.Start();
        auth.OpenBrowser();


        Console.ReadLine();
        auth.Stop(0);

    }

    private static async void AuthOnAuthReceived(object sender, 
    AuthorizationCode payload)
    {
        AuthorizationCodeAuth auth = (AuthorizationCodeAuth)sender;
        auth.Stop();

        Token token = await auth.ExchangeCode(payload.Code);
        SpotifyWebAPI api = new SpotifyWebAPI
        {
            AccessToken = token.AccessToken,
            TokenType = token.TokenType
        };
        PrintUsefulData(api);
    }

    private static async void PrintUsefulData(SpotifyWebAPI api)
    {

        RestClient rClient = new RestClient();
        rClient.endPoint = "https://api.spotify.com/v1/albums";
        string strResponse = string.Empty;
        strResponse = rClient.getRequest();

    }

}

}

public enum HttpVerb
{
    GET,
    POST,
    PUT,
    DELETE
}

class RestClient
{
    public string endPoint { get; set; }
    public HttpVerb httpMethod { get; set; }

    public RestClient()
    {
        endPoint = string.Empty;
        httpMethod = HttpVerb.GET;
    }

    public string getRequest()
    {
        string strResponseVal = string.Empty;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
        request.Method = httpMethod.ToString();

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new ApplicationException("error code: " + response.StatusCode);
            }
            using (Stream responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        strResponseVal = reader.ReadToEnd();
                    }
                }
            }
        }
        return strResponseVal;
    }
}

}

【问题讨论】:

  • 所以,你一路设置一个SpotifyWebAPI 对象和一个(我认为是有效的)访问令牌等等,然后将这个SpotifyWebAPI 对象提供给PrintUsefulData 方法,然后.... 嗯,我忘了我在说什么... ;-)

标签: c# api spotify


【解决方案1】:

这里可能会发生一些事情,但这是我根据您发布的代码提供帮助的机会:

  • 令牌过期 - 如果您在请求中收到 401 错误,那么您需要使用应该在授权时提供的 Refresh Token 来获取新的 @987654322 @。这应该适用于您对 API 进行的任何调用。

  • 请求参数 - 您正在调用的端点 (https://api.spotify.com/v1/albums) 需要参数 ids,因此 Spotify API 知道 您的“专辑”想回来。 更多信息:https://developer.spotify.com/documentation/web-api/reference/albums/get-several-albums/

要检查的另一件事: - 范围 - 确保在对 API 进行身份验证时设置所需的范围,以便将来执行操作。我认为这不适用于这种情况,但值得注意。

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 2013-02-18
    • 2018-08-12
    • 2017-12-23
    • 2022-08-07
    相关资源
    最近更新 更多