/// <summary> /// Returns the refresh code given the <paramref name="credentials"/> and the <paramref name="accessCode"/>. /// </summary> /// <param name="credentials">The oauth credentials.</param> /// <param name="accessCode">The access code returned from the login flow.</param> public static async Task<string> EndOAuthFlow(OAuthCredentials credentials, string redirectUrl, string accessCode) { var client = new WebClient(); var form = new NameValueCollection { { CodeValue, accessCode }, { ClientIdKey, credentials.ClientId }, { ClientSecretKey, credentials.ClientSecret }, { RedirectUriKey, redirectUrl }, { GrantTypeKey, AuthorizationCodeValue }, }; try { var response = await client.UploadValuesTaskAsync(OAuthApiUrl, form); var decoded = Encoding.UTF8.GetString(response); var model = JsonConvert.DeserializeObject<IDictionary<string, string>>(decoded); return model[RefreshTokenKey]; } catch (WebException ex) { Debug.WriteLine($"Failed to finalize oauth flow: {ex.Message}"); throw new OAuthException(ex.Message, ex); } catch (JsonException ex) { Debug.WriteLine($"Failed to parse result: {ex.Message}"); throw new OAuthException(ex.Message, ex); } }
/// <summary> /// Returns the URL to use to start the OAUTH login flow. /// </summary> /// <param name="credentials"></param> /// <param name="scopes"></param> public static string GetInitialOAuthUrl(OAuthCredentials credentials, string redirectUrl, IEnumerable<string> scopes) { var form = new Dictionary<string, string> { { ResponseTypeKey, CodeValue }, { ClientIdKey, credentials.ClientId }, { RedirectUriKey, redirectUrl }, { ScopeKey, String.Join(ScopesSeparator, scopes) }, }; return $"https://accounts.google.com/o/oauth2/auth?{ToQueryString(form)}"; }
private OAuthLoginFlowWindow(OAuthCredentials credentials, IEnumerable<string> scopes) : base(GoogleCloudExtension.Resources.OAuthFlowWindowTitle) { _flow = new OAuthLoginFlow( credentials, scopes, successUrl: SucessUrl, failureUrl: FailureUrl); ViewModel = new OAuthLoginFlowViewModel(this); var windowContent = new OauthLoginFlowWindowContent { DataContext = ViewModel, }; Content = windowContent; StartLoginFlow(); }
public static string PromptUser(OAuthCredentials credentials, IEnumerable<string> scopes) { var dialog = new OAuthLoginFlowWindow(credentials, scopes); dialog.ShowModal(); return dialog.ViewModel.RefreshCode; }