Skip to content

Commit c1c9c15

Browse files
authored
Update packages (#17)
1 parent 3ebf19b commit c1c9c15

File tree

2 files changed

+47
-63
lines changed

2 files changed

+47
-63
lines changed

Git.hub/Client.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void setCredentials(string user, string password)
4646
/// <param name="token">oauth2-token</param>
4747
public void setOAuth2Token(string token)
4848
{
49-
_client.Authenticator = token != null ? new OAuth2AuthHelper(token) : null;
49+
_client.Authenticator = token != null ? new OAuth2AuthorizationRequestHeaderAuthenticator(token, "bearer") : null;
5050
}
5151

5252
/// <summary>
@@ -126,7 +126,7 @@ public IList<Repository> getOrganizationRepositories(string organization)
126126

127127
/// <summary>
128128
/// Retrieves the current user.
129-
///
129+
///
130130
/// Requires to be logged in (OAuth/User+Password).
131131
/// </summary>
132132
/// <returns>current user</returns>

Git.hub/OAuth2Helper.cs

+45-61
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,46 @@
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)
5829
{
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

Comments
 (0)