2017-02-08 185 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登錄屏幕,所以我認爲您不需要在表單中輸入用戶名/密碼。只需一個登錄按鈕即可。