2

I am attempting to create a nice little Spotify playlist creator that anyone will be able to use, i'm trying to seperate the logic out slightly so I have a method for Authorization and Creating a playlist. I can't make a playlist until authorization has occurred, pretty obvious.

My code at the moment looks like this:

    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);
    }

the .Authorize() method authorizes the user but it takes them to a web page, therefore the calling code below runs before the authorization has occurred which throws an exception.

    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
        };
    }

My previous solution was to add the create playlist at the last line of AuthOnAuthReceivedbut i want to seperate the logic out.

Is there a way I can wait for authentication?

  • This is very broad, you might want to look at something like https://stackoverflow.com/questions/6306168/how-to-sleep-a-thread-until-callback-for-asynchronous-function-is-received – Progman Apr 13 '19 at 17:57

0 Answers0