/// <summary> /// Start the authroization code flow or request for an access token if a refresh token is present and the scopes match. /// </summary> /// <param name="cancel">A <see cref="CancellationToken"/> to cancel the wait for users to authorize on their browsers</param> /// <exception cref="OperationCanceledException">Thrown if the wait is canceled</exception> public override async Task Authorize(CancellationToken cancel = default) { AuthorizationCodeTokenResponse tokenResponse; if (RefreshToken.IsNullOrEmpty() || !requiredScopes.IsSubsetOf(AuthorizedScopes)) { var taskCompletionSource = new TaskCompletionSource <AuthorizationCodeResponse>(); EmbedIOAuthServer _server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000); await _server.Start(); _server.AuthorizationCodeReceived += (_, response) => { taskCompletionSource.SetResult(response); return(Task.CompletedTask); }; var request = new SpotifyAPI.Web.LoginRequest(_server.BaseUri, ClientId, SpotifyAPI.Web.LoginRequest.ResponseType.Code) { Scope = requiredScopes }; Helper.OpenUri(request.ToUri()); while (!taskCompletionSource.Task.IsCompleted) { cancel.ThrowIfCancellationRequested(); await Task.Delay(500); } await _server.Stop(); var response = taskCompletionSource.Task.Result; tokenResponse = await new OAuthClient().RequestToken( new AuthorizationCodeTokenRequest( ClientId, ClientSecret, response.Code, new Uri("http://localhost:5000/callback") ) ); RefreshToken = tokenResponse.RefreshToken; } else { var response = await new OAuthClient().RequestToken(new AuthorizationCodeRefreshRequest(ClientId, ClientSecret, RefreshToken)); tokenResponse = new AuthorizationCodeTokenResponse() { RefreshToken = RefreshToken, AccessToken = response.AccessToken, CreatedAt = response.CreatedAt, ExpiresIn = response.ExpiresIn, Scope = response.Scope, TokenType = response.TokenType }; } AccessToken = tokenResponse.AccessToken; AuthorizedScopes = tokenResponse.Scope.Split(' ').ToList(); var config = SpotifyClientConfig .CreateDefault() .WithAuthenticator(new AuthorizationCodeAuthenticator(ClientId, ClientSecret, tokenResponse)); spotify = new SpotifyClient(tokenResponse.AccessToken); RaiseConfigUpdated(EventArgs.Empty); }
/// <summary> /// Start the authroization code flow or request for an access token if a refresh token is present and the scopes match. /// </summary> /// <param name="cancel">A <see cref="CancellationToken"/> to cancel the wait for users to authorize on their browsers</param> /// <exception cref="OperationCanceledException">Thrown if the wait is canceled</exception> public override async Task Authorize(CancellationToken cancel = default) { OsuTokenResponse tokenResponse; if (RefreshToken.IsNullOrEmpty() || !requiredScopes.IsSubsetOf(AuthorizedScopes)) { var taskCompletionSource = new TaskCompletionSource <AuthorizationCodeResponse>(); EmbedIOAuthServer _server = new EmbedIOAuthServer(new Uri("http://localhost:5001/callback"), 5001); await _server.Start(); _server.AuthorizationCodeReceived += (_, response) => { taskCompletionSource.SetResult(response); return(Task.CompletedTask); }; var request = new LoginRequest(new Uri("https://osu.ppy.sh/oauth/authorize"), _server.BaseUri, ClientId, LoginRequest.ResponseType.Code) { Scope = requiredScopes }; Helper.OpenUri(request.ToUri()); while (!taskCompletionSource.Task.IsCompleted) { cancel.ThrowIfCancellationRequested(); await Task.Delay(500); } await _server.Stop(); var response = taskCompletionSource.Task.Result; IRestClient osuAuthClient = new RestClient("https://osu.ppy.sh/").UseNewtonsoftJson(); osuAuthClient.AddHandler("application/json", () => new JsonDeserializer()); var tokenRequest = new RestRequest("oauth/token/", Method.POST); tokenRequest.AddParameter("client_id", ClientId); tokenRequest.AddParameter("client_secret", ClientSecret); tokenRequest.AddParameter("code", response.Code); tokenRequest.AddParameter("grant_type", "authorization_code"); tokenRequest.AddParameter("redirect_uri", "http://localhost:5001/callback"); var codeResponse = await osuAuthClient.ExecuteAsync <OsuTokenResponse>(tokenRequest); tokenResponse = codeResponse.Data; } else { IRestClient osuAuthClient = new RestClient("https://osu.ppy.sh/").UseNewtonsoftJson(); osuAuthClient.AddHandler("application/json", () => new JsonDeserializer()); var tokenRequest = new RestRequest("oauth/token/", Method.POST); tokenRequest.AddParameter("client_id", ClientId); tokenRequest.AddParameter("client_secret", ClientSecret); tokenRequest.AddParameter("refresh_token", RefreshToken); tokenRequest.AddParameter("grant_type", "refresh_token"); var codeResponse = await osuAuthClient.ExecuteAsync <OsuTokenResponse>(tokenRequest); tokenResponse = codeResponse.Data; } RefreshToken = tokenResponse.refresh_token; AccessToken = tokenResponse.access_token; AuthorizedScopes = new List <string>(requiredScopes); osu = new RestClient("https://osu.ppy.sh/").UseNewtonsoftJson(); osu.AddDefaultHeader("Authorization", "Bearer " + tokenResponse.access_token); RaiseConfigUpdated(EventArgs.Empty); }