/// <summary> /// Authenticates a user to enable the user data APIs. /// </summary> /// <param name="client">The MixRadio client.</param> /// <param name="clientSecret">The client secret obtained during app registration</param> /// <param name="scopes">The scopes requested.</param> /// <param name="oauthRedirectUri">The OAuth completed URI.</param> /// <param name="cancellationToken">The cancellation token to cancel operation</param> /// <returns> /// An AuthResultCode value indicating the result /// </returns> /// <remarks> /// Sorry, this method is messy due to the platform differences! /// </remarks> public static async Task <AuthResultCode> AuthenticateUserAsync(this MusicClient client, string clientSecret, Scope scopes, string oauthRedirectUri = MusicClient.DefaultOAuthRedirectUri, CancellationToken?cancellationToken = null) { if (string.IsNullOrEmpty(oauthRedirectUri)) { throw new ArgumentNullException("oauthRedirectUri", "You must supply your OAuth Redirect URI to allow user interaction"); } if (string.IsNullOrEmpty(clientSecret)) { throw new ArgumentNullException("clientSecret", "You must supply your app client secret obtained during app registration"); } // See if we have a cached token... AuthResultCode cachedResult = await AuthenticateUserAsync(client, clientSecret, cancellationToken).ConfigureAwait(false); if (cachedResult == AuthResultCode.Success) { return(cachedResult); } #if WINDOWS_PHONE_APP WebAuthenticationBroker.AuthenticateAndContinue(client.GetAuthenticationUri(scopes), new Uri(oauthRedirectUri)); return(AuthResultCode.InProgress); #elif WINDOWS_APP var authResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, client.GetAuthenticationUri(scopes), new Uri(oauthRedirectUri)); return(await CompleteAuthenticateUserAsync(client, clientSecret, authResult, cancellationToken)); #endif }
/// <summary> /// Authenticates a user to enable the user data APIs. /// </summary> /// <param name="client">The MixRadio client.</param> /// <param name="clientSecret">The client secret obtained during app registration</param> /// <param name="scopes">The scopes requested.</param> /// <param name="browser">The browser control to use to drive authentication.</param> /// <param name="cancellationToken">The optional cancellation token.</param> /// <param name="oauthRedirectUri">The OAuth completed URI.</param> /// <returns> /// An AuthResultCode value indicating the result /// </returns> /// <exception cref="System.ArgumentNullException"> /// browser;You must supply a web browser to allow user interaction /// or /// clientSecret;You must supply your app client secret obtained during app registration /// </exception> /// <remarks> /// Sorry, this method is messy due to the platform differences /// </remarks> public static async Task <AuthResultCode> AuthenticateUserAsync(this MusicClient client, string clientSecret, Scope scopes, WebBrowser browser, CancellationToken?cancellationToken = null, string oauthRedirectUri = MusicClient.DefaultOAuthRedirectUri) { if (browser == null) { throw new ArgumentNullException("browser", "You must supply a web browser to allow user interaction"); } if (string.IsNullOrEmpty(clientSecret)) { throw new ArgumentNullException("clientSecret", "You must supply your app client secret obtained during app registration"); } // See if we have a cached token... AuthResultCode cachedResult = await AuthenticateUserAsync(client, clientSecret, cancellationToken).ConfigureAwait(false); if (cachedResult == AuthResultCode.Success) { return(cachedResult); } var browserController = new OAuthBrowserController(); CancellationToken token = cancellationToken ?? CancellationToken.None; await Task.Run(() => browserController.DriveAuthProcess(browser, client.GetAuthenticationUri(scopes), oauthRedirectUri, token), token); AuthResultCode authResult = browserController.ResultCode; if (authResult != AuthResultCode.Cancelled) { // Grab the results and kill the browser controller string code = browserController.AuthorizationCode; browserController = null; // Move on to obtain a token if (authResult == AuthResultCode.Success) { var authToken = await client.GetAuthenticationTokenAsync(clientSecret, code, cancellationToken); if (authToken != null) { await StoreOAuthToken(authToken, client.ClientId, clientSecret, cancellationToken).ConfigureAwait(false); } } } return(authResult); }