我正在尝试创建一个不错的小型Spotify播放列表创建者,任何人都可以使用,我试图将逻辑略微分开,所以我有一种授权和创建播放列表的方法。很明显,在获得授权之前,我无法创建播放列表。
此刻我的代码如下:
static void Main(string[] args)
{
PlaylistProvider provider = new PlaylistProvider(clientID:"",
secretID: "", playlistID: "");
bool authorized = provider.Authorize();
List<string> artists = new List<string>();
artists.Add("stub");
provider.CreatePlaylist(artists, PlaylistType.CONCERT);
}
.Authorize()方法授权用户,但它将用户带到网页,因此下面的调用代码在授权发生之前运行,从而引发异常。
public bool Authorize()
{
AuthorizationCodeAuth auth =
new AuthorizationCodeAuth(ClientId, SecretId, "http://localhost:4003", "http://localhost:4003",
Scope.PlaylistModifyPrivate | Scope.PlaylistReadCollaborative);
//Once auth has been received, call AuthOnAuthReceieved method
auth.AuthReceived += AuthOnAuthReceived;
auth.Start();
auth.OpenBrowser();
return true;
}
private static async void AuthOnAuthReceived(object sender, AuthorizationCode payload)
{
AuthorizationCodeAuth auth = (AuthorizationCodeAuth)sender;
auth.Stop();
Token token = await auth.ExchangeCode(payload.Code);
SpotifyWebAPI = new SpotifyWebAPI
{
AccessToken = token.AccessToken,
TokenType = token.TokenType
};
}
我以前的解决方案是将创建播放列表添加到AuthOnAuthReceived的最后一行,但我想将逻辑分开。
我可以等待验证吗?