public void TestGetAccessToken() { var clientId = this.fixture.Config.ClientId; var clientSecret = this.fixture.Config.ClientSecret; var redirectUrl = this.fixture.Config.RedirectUrl; var authUsername = this.fixture.Config.AuthUsername; var authPassword = this.fixture.Config.AuthPassword; var authWorkspace = this.fixture.Config.AuthWorkspace; using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))) { driver.Navigate().GoToUrl($"https://{authWorkspace}.slack.com"); // Wait a bit to ensure we can properly fill username and password fields Thread.Sleep(1000); driver.FindElement(By.Id("email")).SendKeys(authUsername); driver.FindElement(By.Id("password")).SendKeys(authPassword); driver.FindElement(By.Id("signin_btn")).Click(); var uri = SlackClient.GetAuthorizeUri(clientId, SlackScope.Identify); driver.Navigate().GoToUrl(uri); driver.FindElement(By.Id("oauth_authorizify")).Click(); var code = Regex.Match(driver.Url, "code=(?<code>[^&]+)&state").Groups["code"].Value; var accessTokenResponse = GetAccessToken(clientId, clientSecret, redirectUrl, code); Assert.True(accessTokenResponse.ok); Assert.Equal("identify", accessTokenResponse.scope); } }
private static string CompleteOAuth() { var uri = SlackClient.GetAuthorizeUri(Constants.CLIENT_ID, SlackScope.Identify | SlackScope.Read | SlackScope.Post, Constants.CALLBACK_URI, STATE, Constants.TEAM_NAME); Console.WriteLine("Launching: " + uri); OpenBrowserHack.OpenBrowser(uri.ToString()); RESULT_PENDING = true; while (RESULT_PENDING) { } return(RESULT); }
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(); } }