I want my app to first get authorization and then do something with it, e.g. display user's name. The following code returns an error.
SpotifyWebApi api;
// (1) Get authorization (code from the official guide)
AuthorizationCodeAuth auth = new AuthorizationCodeAuth(
"http://localhost:4002",
"http://localhost:4002",
Scope.UserReadPrivate | Scope.UserReadEmail
);
auth.AuthReceived += async (sender, payload) =>
{
auth.Stop();
Token token = await auth.ExchangeCode(payload.Code);
api = new SpotifyWebAPI()
{
TokenType = token.TokenType,
AccessToken = token.AccessToken
};
};
auth.Start();
auth.OpenBrowser();
// (2) Get user's data
PrivateProfile apiUser = api.GetPrivateProfile();
Console.WriteLine(apiUser.DisplayName);
The problem is that the (2)
code executes before it obtains the necessary authorization that (1)
should provide, therefore, api.GetPrivateProfile()
returns null
. What should I do?