private async Task AuthenticateAsync() { SetRequestTokenForSessionIdResponse postRequestTokenForSessionIdResponse = null; if (string.IsNullOrWhiteSpace(_sessionId)) { var requestTokenResponse = await GetRequestTokenAsync(); AuthenticationInformationsAvailable?.Invoke(this, new AuthenticationInformations { AuthenticationUrl = $"https://www.themoviedb.org/authenticate/{requestTokenResponse.request_token}" }); while (postRequestTokenForSessionIdResponse == null) { try { postRequestTokenForSessionIdResponse = await SetRequestTokenForSessionIdAsync(new SetRequestTokenForSessionIdBody { request_token = requestTokenResponse.request_token }); _sessionId = postRequestTokenForSessionIdResponse.session_id; } catch (Exception) { await Task.Delay(2000); } } } _httpService.AddQueryParameters($"&session_id={_sessionId}"); AuthenticationSuccessfull?.Invoke(this, new AuthenticationResult { SessionId = _sessionId }); }
private async Task AuthenticateAsync() { if (string.IsNullOrWhiteSpace(_refreshToken)) { var postClientIdForDeviceCodeResponse = await PostClientIdForDeviceCodeAsync(_clientId); PostDeviceCodeForAccessTokenResponse postDeviceCodeForAccessTokenResponse = null; AuthenticationInformationsAvailable?.Invoke(this, new AuthenticationInformations { AuthenticationUrl = postClientIdForDeviceCodeResponse.verification_url, DeviceCode = postClientIdForDeviceCodeResponse.user_code }); while (postDeviceCodeForAccessTokenResponse == null) { await Task.Delay(TimeHelper.FromSecondsToMilliseconds(postClientIdForDeviceCodeResponse.interval)); try { postDeviceCodeForAccessTokenResponse = await PostDeviceCodeForAccessTokenAsync(postClientIdForDeviceCodeResponse.device_code, _clientId, _clientSecret); _accessToken = postDeviceCodeForAccessTokenResponse.access_token; _refreshToken = postDeviceCodeForAccessTokenResponse.refresh_token; } catch (Exception) { } } } else { var postRefreshTokenForAccessTokenResponse = await PostRefreshTokenForAccessTokenAsync(_refreshToken); _accessToken = postRefreshTokenForAccessTokenResponse.access_token; _refreshToken = postRefreshTokenForAccessTokenResponse.refresh_token; } _httpService.AddHeader("Authorization", $"Bearer {_accessToken}"); AuthenticationSuccessfull?.Invoke(this, new AuthenticationResult { AccessToken = _accessToken, RefreshToken = _refreshToken }); }