我在c#wpf

时间:2019-02-12 20:11:09

标签: c# api spotify

我收到此错误。 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;
    }
}

}

1 个答案:

答案 0 :(得分:0)

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

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

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

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