|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Linq; |
4 |
| -using System.Text; |
5 |
| -using RestSharp; |
6 |
| -using RestSharp.Authenticators; |
7 |
| - |
8 |
| -namespace Git.hub |
9 |
| -{ |
10 |
| - class OAuth2Data |
11 |
| - { |
12 |
| - public string AccessToken { get; private set; } |
13 |
| - public string TokenType { get; private set; } |
14 |
| - } |
15 |
| - |
16 |
| - public class OAuth2Helper |
17 |
| - { |
18 |
| - /// <summary> |
19 |
| - /// Requests a Github API token, given you received a code first, i.e. user allowed that. |
20 |
| - /// |
21 |
| - /// See OAuth documentation for how to do that. |
22 |
| - /// Your App needs to be registered first. |
23 |
| - /// </summary> |
24 |
| - /// <param name="client_id">your app's client_id</param> |
25 |
| - /// <param name="client_secret">your app's secret</param> |
26 |
| - /// <param name="code">code you got from github</param> |
27 |
| - /// <returns>oauth token if successful, null otherwise</returns> |
28 |
| - public static string requestToken(string client_id, string client_secret, string code) |
29 |
| - { |
30 |
| - // Not on api.github.com |
31 |
| - var client = new RestClient("https://github.com"); |
32 |
| - client.UserAgent = "mabako/Git.hub"; |
33 |
| - |
34 |
| - var request = new RestRequest("/login/oauth/access_token"); |
35 |
| - request.RequestFormat = DataFormat.Json; |
36 |
| - request.AddParameter("client_id", client_id); |
37 |
| - request.AddParameter("client_secret", client_secret); |
38 |
| - request.AddParameter("code", code); |
39 |
| - |
40 |
| - var response = client.Post<OAuth2Data>(request); |
41 |
| - if (response.Data != null) |
42 |
| - return response.Data.AccessToken; |
43 |
| - return null; |
44 |
| - } |
45 |
| - } |
46 |
| - |
47 |
| - /// <summary> |
48 |
| - /// Github sure has some quirks. |
49 |
| - /// |
50 |
| - /// (not even sure why) Using RestSharp's authenticators works on GET /user, but not GET /user/repos? |
51 |
| - /// This basically works around that. |
52 |
| - /// </summary> |
53 |
| - class OAuth2AuthHelper : AuthenticatorBase |
54 |
| - { |
55 |
| - public OAuth2AuthHelper(string token) : base(token) { } |
56 |
| - |
57 |
| - protected override Parameter GetAuthenticationParameter(string accessToken) |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using RestSharp; |
| 6 | +using RestSharp.Authenticators; |
| 7 | + |
| 8 | +namespace Git.hub |
| 9 | +{ |
| 10 | + class OAuth2Data |
| 11 | + { |
| 12 | + public string AccessToken { get; private set; } |
| 13 | + public string TokenType { get; private set; } |
| 14 | + } |
| 15 | + |
| 16 | + public class OAuth2Helper |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Requests a Github API token, given you received a code first, i.e. user allowed that. |
| 20 | + /// |
| 21 | + /// See OAuth documentation for how to do that. |
| 22 | + /// Your App needs to be registered first. |
| 23 | + /// </summary> |
| 24 | + /// <param name="client_id">your app's client_id</param> |
| 25 | + /// <param name="client_secret">your app's secret</param> |
| 26 | + /// <param name="code">code you got from github</param> |
| 27 | + /// <returns>oauth token if successful, null otherwise</returns> |
| 28 | + public static string requestToken(string client_id, string client_secret, string code) |
58 | 29 | {
|
59 |
| - return new Parameter("Authorization", "bearer " + accessToken, ParameterType.HttpHeader); |
60 |
| - } |
61 |
| - } |
62 |
| -} |
| 30 | + // Not on api.github.com |
| 31 | + var client = new RestClient("https://github.com"); |
| 32 | + client.UserAgent = "mabako/Git.hub"; |
| 33 | + |
| 34 | + var request = new RestRequest("/login/oauth/access_token"); |
| 35 | + request.RequestFormat = DataFormat.Json; |
| 36 | + request.AddParameter("client_id", client_id); |
| 37 | + request.AddParameter("client_secret", client_secret); |
| 38 | + request.AddParameter("code", code); |
| 39 | + |
| 40 | + var response = client.Post<OAuth2Data>(request); |
| 41 | + if (response.Data != null) |
| 42 | + return response.Data.AccessToken; |
| 43 | + return null; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments