2017-02-08 110 views
0

我正在试图在使用this .Net Wrapper/API的C#表单中使用Spotify的Web API。Spotify API访问和刷新令牌

我想创建一个表单,用户可以输入他的用户名和密码,然后程序创建一个播放列表(使用授权码流),但我在验证失败。我在Web Api网站上创建了一个项目并保存了我的ClientID和ClientSecret,但是如何获取访问令牌和刷新令牌?该API没有提供这方面的例子,我无法找到解决方案。

using SpotifyWebAPI; 
using System; 
using System.Windows.Forms; 

namespace Spotify_Extender 
{ 
    public partial class Main : Form 
    { 
     public Main() 
     { 
      InitializeComponent(); 
      initialize(); 
     } 

     private async void initialize() 
     { 
      var AuthenticationToken = new AuthenticationToken() 
      { 
       AccessToken = "", 
       ExpiresOn = DateTime.Now.AddSeconds(3600), 
       RefreshToken = "", 
       TokenType = "Bearer" 
      }; 

      // In a example the guy who made the API uses the Auth-Token to get the User, 
      // so i assume up there you have to combine your clientToken with the credentials 
      // or something like that 
      var user = await SpotifyWebAPI.User.GetCurrentUserProfile(AuthenticationToken); 

      MessageBox.Show(user.EmailAddress); 

      var playlists = await user.GetPlaylists(AuthenticationToken); 
     } 
    } 
} 

回答

0

参考https://developer.spotify.com/web-api/authorization-guide/

将有几个步骤,你可以得到访问令牌和刷新令牌之前。

  1. 请求授权
  2. 登录/验证访问
  3. 重定向回(有授权码返回)
  4. 请求访问令牌和刷新令牌(需要授权代码)

通过创建一个授权访问,您将被重定向到Spotify登录屏幕,所以我认为您不需要在表单中输入用户名/密码。只需一个登录按钮即可。