Skip to content

Commit 1e140ba

Browse files
committed
fixing some Github OAuth quirks
1 parent 1585670 commit 1e140ba

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

Git.hub.Demo/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ static void Main(string[] args)
1313
Client client = new Client();
1414

1515
client.setCredentials("mabako", "");
16+
//client.setOAuth2Token("");
1617
Console.WriteLine("Logged in as: {0}", client.getCurrentUser());
18+
client.getRepositories().ToList().ForEach(repo => Console.WriteLine(" {0}", repo.Name));
1719

1820
Console.WriteLine();
1921
Console.WriteLine("Repositories of mabako?");

Git.hub/Client.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void setCredentials(string user, string password)
3737
public void setOAuth2Token(string token)
3838
{
3939
if (token != null)
40-
client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(token);
40+
client.Authenticator = new OAuth2AuthHelper(token);
4141
else
4242
client.Authenticator = null;
4343
}
@@ -53,9 +53,12 @@ public IList<Repository> getRepositories()
5353

5454
var request = new RestRequest("/user/repos?type=all");
5555

56-
var list = client.Get<List<Repository>>(request).Data;
57-
list.ForEach(r => r._client = client);
58-
return list;
56+
var repos = client.Get<List<Repository>>(request);
57+
if (repos.Data == null)
58+
throw new Exception("Bad Credentials");
59+
60+
repos.Data.ForEach(r => r._client = client);
61+
return repos.Data;
5962
}
6063

6164
/// <summary>
@@ -123,7 +126,12 @@ public User getCurrentUser()
123126
throw new ArgumentException("no authentication details");
124127

125128
var request = new RestRequest("/user");
126-
return client.Get<User>(request).Data;
129+
130+
var user = client.Get<User>(request);
131+
if (user.Data == null)
132+
throw new Exception("Bad Credentials");
133+
134+
return user.Data;
127135
}
128136
}
129137
}

Git.hub/OAuth2Helper.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,20 @@ public static string requestToken(string client_id, string client_secret, string
4242
return null;
4343
}
4444
}
45+
46+
/// <summary>
47+
/// Github sure has some quirks.
48+
///
49+
/// (not even sure why) Using RestSharp's authenticators works on GET /user, but not GET /user/repos?
50+
/// This basically works around that.
51+
/// </summary>
52+
class OAuth2AuthHelper : OAuth2Authenticator
53+
{
54+
public OAuth2AuthHelper(string token) : base(token) { }
55+
56+
public override void Authenticate(IRestClient client, IRestRequest request)
57+
{
58+
request.AddHeader("Authorization", "bearer " + AccessToken);
59+
}
60+
}
4561
}

0 commit comments

Comments
 (0)