public static SlackClient GetSlackClient(string oAuthToken) { SlackClient client = null; var complete = false; try { Action <AccessTokenResponse> callback = response => { Console.WriteLine( string.IsNullOrWhiteSpace(response.access_token) ? "AccessToken was not provided. Error reason: " + response.error : "Got access token '{0}'...", response.access_token); client = new SlackClient(response.access_token); Console.WriteLine("Done."); complete = true; }; Console.WriteLine("Requesting access token..."); SlackClient.GetAccessToken(callback, Constants.CLIENT_ID, Constants.CLIENT_SECRET, Constants.CALLBACK_URI, oAuthToken); Console.WriteLine("Awaiting results of the AccessToken callback"); while (!complete) { } ; } catch (Exception ex) { Console.WriteLine(ex); } return(client); }
/// <summary> /// Called with temporary token. Will be used to generate authorization token and persisted for future use /// </summary> /// <param name="code">tempory token</param> /// <param name="state">Not used but included for future development</param> public void Get(string code, string state = "") { try { // Like await - but old school var ewh = new EventWaitHandle(false, EventResetMode.ManualReset, code); SlackClient.GetAccessToken(response => { using (var da = new DataAccess()) { da.InsertAuthorization(new AuthorizationEntity { AccessToken = response.access_token, Channel = response.incoming_webhook.channel, ChannelId = response.incoming_webhook.channel_id, TeamId = response.team_id, TeamName = response.team_name, Url = response.incoming_webhook.url }.SetKeys() ); } ewh.Set(); }, ClientId, ClientSecret, "", code); ewh.WaitOne(50000); } catch (Exception e) { var error = e; } }
private AccessTokenResponse GetAccessToken(string clientId, string clientSecret, string redirectUri, string authCode) { AccessTokenResponse accessTokenResponse = null; using (var sync = new InSync(nameof(SlackClient.GetAccessToken))) { SlackClient.GetAccessToken(response => { accessTokenResponse = response; sync.Proceed(); }, clientId, clientSecret, redirectUri, authCode); } return(accessTokenResponse); }
private AccessTokenResponse GetAccessToken(string clientId, string clientSecret, string redirectUri, string authCode) { var waiter = new EventWaitHandle(false, EventResetMode.ManualReset); AccessTokenResponse accessTokenResponse = null; SlackClient.GetAccessToken(response => { accessTokenResponse = response; waiter.Set(); }, clientId, clientSecret, redirectUri, authCode); Policy .Handle <AssertFailedException>() .WaitAndRetry(15, x => TimeSpan.FromSeconds(0.2), (exception, span) => Console.WriteLine("Retrying in {0} seconds", span.TotalSeconds)) .Execute(() => { Assert.IsTrue(waiter.WaitOne(), "Still waiting for things to happen..."); }); return(accessTokenResponse); }
static void Main(string[] args) { try { var clientId = "PUT CLIENT ID FROM SLACK APPLICATION REGISTATION HERE"; var clientSecret = "PUT CLIENT SECRET FROM SLACK APPLICATION REGISTATION HERE"; var redirectUri = "PUT REDIRECT FROM SLACK APPLICATION REGISTATION HERE"; Console.WriteLine("------------------------------------------------------------------"); Console.WriteLine("This app will open your web browser pointing at an authentication"); Console.WriteLine("page. When you complete authentication, you'll be sent back to "); Console.WriteLine("whatever 'redirectUri' is above, plus some query-string values. "); Console.WriteLine("Paste the URI into the console window when prompted."); Console.WriteLine(); Console.WriteLine("In a proper web application, the user experience will obviously"); Console.WriteLine("be more sensible..."); Console.WriteLine("------------------------------------------------------------------"); // start... var state = Guid.NewGuid().ToString(); var uri = SlackClient.GetAuthorizeUri(clientId, SlackScope.Identify | SlackScope.Read | SlackScope.Post, redirectUri, state, "socialsaleslounge"); Console.WriteLine("Directing to: " + uri); Process.Start(uri.ToString()); // read the result -- in a web application you can pick this up directly, here we're fudging it... Console.WriteLine("Paste in the URL of the authentication result..."); var asString = Console.ReadLine(); var index = asString.IndexOf('?'); if (index != -1) { asString = asString.Substring(index + 1); } // parse... var qs = HttpUtility.ParseQueryString(asString); var code = qs["code"]; var newState = qs["state"]; // validate the state. this isn't required, but it's makes sure the request and response line up... if (state != newState) { throw new InvalidOperationException("State mismatch."); } // then get the token... Console.WriteLine("Requesting access token..."); SlackClient.GetAccessToken((response) => { var accessToken = response.access_token; Console.WriteLine("Got access token '{0}'...", accessToken); // post... var client = new SlackClient(accessToken); client.PostMessage(null, "#registrations", "Test", "Jo the Robot"); }, clientId, clientSecret, redirectUri, code); // finished... Console.WriteLine("Done."); } catch (Exception ex) { Console.WriteLine(ex); } finally { Console.ReadLine(); } }