-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathProgram.cs
More file actions
75 lines (65 loc) · 3.26 KB
/
Program.cs
File metadata and controls
75 lines (65 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Diagnostics;
using System.Web;
namespace SlackAPI
{
class Program
{
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 | SlackScope.Client, 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();
}
}
}
}