/// <summary> /// Creates a GlimeshConnection object from an OAuth authentication locally. /// </summary> /// <param name="clientID">The ID of the client application</param> /// <param name="clientSecret">The secret of the client application</param> /// <param name="scopes">The authorization scopes to request</param> /// <param name="forceApprovalPrompt">Whether to force an approval from the user</param> /// <param name="oauthListenerURL">The URL to listen for the OAuth successful authentication</param> /// <param name="successResponse">The response to send back upon successful authentication</param> /// <returns>The GlimeshConnection object</returns> public static async Task <GlimeshConnection> ConnectViaLocalhostOAuthBrowser(string clientID, string clientSecret, IEnumerable <OAuthClientScopeEnum> scopes, bool forceApprovalPrompt = false, string oauthListenerURL = DEFAULT_OAUTH_LOCALHOST_URL, string successResponse = null) { Validator.ValidateString(clientID, "clientID"); Validator.ValidateList(scopes, "scopes"); LocalOAuthHttpListenerServer oauthServer = new LocalOAuthHttpListenerServer(oauthListenerURL, DEFAULT_AUTHORIZATION_CODE_URL_PARAMETER, successResponse); oauthServer.Start(); string url = await GlimeshConnection.GetAuthorizationCodeURLForOAuthBrowser(clientID, scopes, oauthListenerURL, forceApprovalPrompt); ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = url, UseShellExecute = true }; Process.Start(startInfo); string authorizationCode = await oauthServer.WaitForAuthorizationCode(); oauthServer.Stop(); if (authorizationCode != null) { return(await GlimeshConnection.ConnectViaAuthorizationCode(clientID, clientSecret, authorizationCode, redirectUrl : oauthListenerURL)); } return(null); }
/// <summary> /// Creates a GlimeshConnection object from an OAuth token. /// </summary> /// <param name="token">The OAuth token to use</param> /// <param name="refreshToken">Whether to refresh the token</param> /// <returns>The GlimeshConnection object</returns> public static async Task <GlimeshConnection> ConnectViaOAuthToken(OAuthTokenModel token, bool refreshToken = true) { Validator.ValidateVariable(token, "token"); GlimeshConnection connection = new GlimeshConnection(token); if (refreshToken) { await connection.RefreshOAuthToken(); } return(connection); }
/// <summary> /// Generates the OAuth authorization URL to use for authentication. /// </summary> /// <param name="clientID">The ID of the client application</param> /// <param name="scopes">The authorization scopes to request</param> /// <param name="redirectUri">The redirect URL for the client application</param> /// <param name="forceApprovalPrompt">Whether to force an approval from the user</param> /// <returns>The authorization URL</returns> public static async Task <string> GetAuthorizationCodeURLForOAuthBrowser(string clientID, IEnumerable <OAuthClientScopeEnum> scopes, string redirectUri, bool forceApprovalPrompt = false) { Validator.ValidateString(clientID, "clientID"); Validator.ValidateList(scopes, "scopes"); Validator.ValidateString(redirectUri, "redirectUri"); Dictionary <string, string> parameters = new Dictionary <string, string>() { { "client_id", clientID }, { "scope", GlimeshConnection.ConvertClientScopesToString(scopes) }, { "response_type", "code" }, { "redirect_uri", redirectUri }, }; if (forceApprovalPrompt) { parameters.Add("force_verify", "force"); } FormUrlEncodedContent content = new FormUrlEncodedContent(parameters.AsEnumerable()); return("https://glimesh.tv/oauth/authorize?" + await content.ReadAsStringAsync()); }