【问题标题】:How to call the Spotify API from C#如何从 C# 调用 Spotify API
【发布时间】:2016-03-04 14:21:11
【问题描述】:

我正在尝试使用 C# 调用 spotify API。 不幸的是,我已经无法获得access token

这就是我试图得到它的方式:

private static async Task<string> GetAccessToken()
        {
            SpotifyToken token = new SpotifyToken();

            string postString = string.Format("grant_type=client_credentials");
            byte[] byteArray = Encoding.UTF8.GetBytes(postString);

            string url = "https://accounts.spotify.com/api/token";

            WebRequest request = WebRequest.Create(url);
            request.Method = "POST";
            request.Headers.Add("Authorization", "Basic {Encoded myClientIdXXX:myAppSecretYYY}");
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
                using (WebResponse response = await request.GetResponseAsync())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            string responseFromServer = reader.ReadToEnd();
                            token = JsonConvert.DeserializeObject<SpotifyToken>(responseFromServer);
                        }
                    }
                }
            }
            return token.access_token;
        }

但我得到一个错误:

Der Remoteserver hat einen Fehler zurückgegeben:(400) Ungültige Anforderung。

翻译为:

远程服务器返回错误:(400) Bad Request。

我在这里做错了什么? 我已经在 spotify 注册了我的应用,并在请求标头中设置了 clientId 和 secret...

提前致谢

【问题讨论】:

  • *远程服务器返回错误:(400)错误请求。
  • 感谢 Taegost 和 @SethKitchen 添加错误消息的英文翻译

标签: c# .net web-services spotify


【解决方案1】:

您真的需要从头开始编写自己的客户端来构建 WebRequest 等吗?为什么不使用像SpotifyAPI-NET 这样的现有客户端?

不管怎样,你可以在 GitHub 上看到他们是如何做到的:

https://github.com/JohnnyCrazy/SpotifyAPI-NET/blob/master/SpotifyAPI/Web/Auth/AutorizationCodeAuth.cs

using Newtonsoft.Json;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace SpotifyAPI.Web.Auth
{
    public class AutorizationCodeAuth
    {
        public delegate void OnResponseReceived(AutorizationCodeAuthResponse response);

        private SimpleHttpServer _httpServer;
        private Thread _httpThread;
        public String ClientId { get; set; }
        public String RedirectUri { get; set; }
        public String State { get; set; }
        public Scope Scope { get; set; }
        public Boolean ShowDialog { get; set; }

        /// <summary>
        ///     Will be fired once the user authenticated
        /// </summary>
        public event OnResponseReceived OnResponseReceivedEvent;

        /// <summary>
        ///     Start the auth process (Make sure the internal HTTP-Server ist started)
        /// </summary>
        public void DoAuth()
        {
            String uri = GetUri();
            Process.Start(uri);
        }

        /// <summary>
        ///     Refreshes auth by providing the clientsecret (Don't use this if you're on a client)
        /// </summary>
        /// <param name="refreshToken">The refresh-token of the earlier gathered token</param>
        /// <param name="clientSecret">Your Client-Secret, don't provide it if this is running on a client!</param>
        public Token RefreshToken(string refreshToken, string clientSecret)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Proxy = null;
                wc.Headers.Add("Authorization",
                    "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + clientSecret)));
                NameValueCollection col = new NameValueCollection
                {
                    {"grant_type", "refresh_token"},
                    {"refresh_token", refreshToken}
                };

                String response;
                try
                {
                    byte[] data = wc.UploadValues("https://accounts.spotify.com/api/token", "POST", col);
                    response = Encoding.UTF8.GetString(data);
                }
                catch (WebException e)
                {
                    using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
                    {
                        response = reader.ReadToEnd();
                    }
                }
                return JsonConvert.DeserializeObject<Token>(response);
            }
        }

        private String GetUri()
        {
            StringBuilder builder = new StringBuilder("https://accounts.spotify.com/authorize/?");
            builder.Append("client_id=" + ClientId);
            builder.Append("&response_type=code");
            builder.Append("&redirect_uri=" + RedirectUri);
            builder.Append("&state=" + State);
            builder.Append("&scope=" + Scope.GetStringAttribute(" "));
            builder.Append("&show_dialog=" + ShowDialog);
            return builder.ToString();
        }

        /// <summary>
        ///     Start the internal HTTP-Server
        /// </summary>
        public void StartHttpServer(int port = 80)
        {
            _httpServer = new SimpleHttpServer(port, AuthType.Authorization);
            _httpServer.OnAuth += HttpServerOnOnAuth;

            _httpThread = new Thread(_httpServer.Listen);
            _httpThread.Start();
        }

        private void HttpServerOnOnAuth(AuthEventArgs e)
        {
            OnResponseReceivedEvent?.Invoke(new AutorizationCodeAuthResponse()
            {
                Code = e.Code,
                State = e.State,
                Error = e.Error
            });
        }

        /// <summary>
        ///     This will stop the internal HTTP-Server (Should be called after you got the Token)
        /// </summary>
        public void StopHttpServer()
        {
            _httpServer = null;
        }

        /// <summary>
        ///     Exchange a code for a Token (Don't use this if you're on a client)
        /// </summary>
        /// <param name="code">The gathered code from the response</param>
        /// <param name="clientSecret">Your Client-Secret, don't provide it if this is running on a client!</param>
        /// <returns></returns>
        public Token ExchangeAuthCode(String code, String clientSecret)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Proxy = null;

                NameValueCollection col = new NameValueCollection
                {
                    {"grant_type", "authorization_code"},
                    {"code", code},
                    {"redirect_uri", RedirectUri},
                    {"client_id", ClientId},
                    {"client_secret", clientSecret}
                };

                String response;
                try
                {
                    byte[] data = wc.UploadValues("https://accounts.spotify.com/api/token", "POST", col);
                    response = Encoding.UTF8.GetString(data);
                }
                catch (WebException e)
                {
                    using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
                    {
                        response = reader.ReadToEnd();
                    }
                }
                return JsonConvert.DeserializeObject<Token>(response);
            }
        }
    }

    public struct AutorizationCodeAuthResponse
    {
        public String Code { get; set; }
        public String State { get; set; }
        public String Error { get; set; }
    }
}

【讨论】:

  • 我尝试使用它,但您无法将 nuget 包添加到针对任何最新版本的 Windows 10 的项目中。我真正想要做的是创建一个 Xamarin 应用程序,但可以找到没有什么可以帮助我从 pcl 授权 Spotify。有人有什么想法吗?
【解决方案2】:

来自 C# 的 Spotify API

public string GetAccessToken()
    {
            SpotifyToken token = new SpotifyToken();
            string url5 = "https://accounts.spotify.com/api/token";
            var clientid = "your_client_id";
            var clientsecret = "your_client_secret";

            //request to get the access token
            var encode_clientid_clientsecret = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", clientid, clientsecret)));

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url5);

            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Accept = "application/json";
            webRequest.Headers.Add("Authorization: Basic " + encode_clientid_clientsecret);

            var request = ("grant_type=client_credentials");
            byte[] req_bytes = Encoding.ASCII.GetBytes(request);
            webRequest.ContentLength = req_bytes.Length;

            Stream strm = webRequest.GetRequestStream();
            strm.Write(req_bytes, 0, req_bytes.Length);
            strm.Close();

            HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
            String json = "";
            using (Stream respStr = resp.GetResponseStream())
            {
                using (StreamReader rdr = new StreamReader(respStr, Encoding.UTF8))
                {
                    //should get back a string i can then turn to json and parse for accesstoken
                    json = rdr.ReadToEnd();
                    rdr.Close();
                }
            }
     return token.access_token;
}

【讨论】:

  • 这是我需要的。谢谢!
【解决方案3】:

只需对您的 App ClientId 和 ClientSecret 进行编码:

var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}",this.CliendId,this.ClientSecret)));

正确更换:

request.Headers.Add("Authorization", "Basic " + token);

就是这样!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2020-02-22
    • 2020-10-10
    • 2014-08-16
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多