如何授权OAuth令牌

时间:2019-03-06 19:31:34

标签: c# authorization

所以,我有以下代码:

而且我不知道如何回发授权令牌(OAuth)来从Spotify获取数据。

我的令牌:授权:承载BQC0GM4YDD3OldzDBvAagXZxC7rfsOnxySlKvVe_F4Kj1IOBSYnX4UO1y4U2a0UbMpqnTsyA3tyOvR7YjcTEkHQmhrqmxTtG6W8_aqxaxE-mCXENZp82sRywOKe2lk6vNCrV6BO7MpAultZy8g

public async Task<string> TestGet()
        {
            string testRequest = "";

            try
            {
                var request = System.Net.HttpWebRequest.CreateHttp(BaseAdress + "?q=Quebonafide&type=artist");
                request.Method = System.Net.WebRequestMethods.Http.Get;
                request.ContentType = "application/json; charset=utf-8";

                await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null)
                    .ContinueWith(Task => {

                        var response = (HttpWebResponse)Task.Result;

                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                            string responseData = responseReader.ReadToEnd();

                            testRequest = responseData.ToString();
                            responseReader.Close();
                        }
                    });

            }
            catch(Exception)
            {

            }
            return testRequest;
        }

1 个答案:

答案 0 :(得分:0)

您应该更新您的请求并添加一个授权标头,如下所示:

request.Headers.Add("Authorization", "Bearer " + accessToken);