Skip to content

Commit f94fefa

Browse files
committed
Add new WebAgent for bots to automatically retrieve a new token upon expiration. Not fully tested.
Add Rebracer support for formatting
1 parent 78e57e5 commit f94fefa

File tree

7 files changed

+323
-6
lines changed

7 files changed

+323
-6
lines changed

Rebracer.xml

Lines changed: 249 additions & 0 deletions
Large diffs are not rendered by default.

RedditSharp.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting", "UnitTesting\
1212
{A368CB75-75F0-4489-904D-B5CEBB0FE624} = {A368CB75-75F0-4489-904D-B5CEBB0FE624}
1313
EndProjectSection
1414
EndProject
15+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{46584E90-4894-4EB7-A0B9-FDAF29B4AF8B}"
16+
ProjectSection(SolutionItems) = preProject
17+
Rebracer.xml = Rebracer.xml
18+
EndProjectSection
19+
EndProject
1520
Global
1621
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1722
Debug|Any CPU = Debug|Any CPU

RedditSharp/AuthProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace RedditSharp
99
{
1010
public class AuthProvider
1111
{
12-
private const string AccessUrl = "https://ssl.reddit.com/api/v1/access_token";
12+
public const string AccessUrl = "https://ssl.reddit.com/api/v1/access_token";
1313
private const string OauthGetMeUrl = "https://oauth.reddit.com/api/v1/me";
1414
private const string RevokeUrl = "https://www.reddit.com/api/v1/revoke_token";
1515

RedditSharp/BotWebAgent.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace RedditSharp
9+
{
10+
public class BotWebAgent : WebAgent
11+
{
12+
//private so it doesn't leak app secret to other code
13+
private AuthProvider TokenProvider;
14+
private string Username;
15+
private string Password;
16+
17+
public DateTime TokenValidTo { get; set; }
18+
19+
public BotWebAgent(string username, string password, string clientID, string clientSecret, string redirectURI)
20+
{
21+
Username = username;
22+
Password = password;
23+
EnableRateLimit = true;
24+
RateLimit = RateLimitMode.Burst;
25+
RootDomain = "oauth.reddit.com";
26+
TokenProvider = new AuthProvider(clientID, clientSecret, redirectURI, this);
27+
GetNewToken();
28+
}
29+
30+
public override HttpWebRequest CreateRequest(string url, string method)
31+
{
32+
//add 5 minutes for clock skew to ensure requests succeed
33+
if (url != AuthProvider.AccessUrl && DateTime.UtcNow.AddMinutes(5) > TokenValidTo)
34+
{
35+
GetNewToken();
36+
}
37+
return base.CreateRequest(url, method);
38+
}
39+
40+
protected override HttpWebRequest CreateRequest(Uri uri, string method)
41+
{
42+
//add 5 minutes for clock skew to ensure requests succeed
43+
if (uri.ToString() != AuthProvider.AccessUrl && DateTime.UtcNow.AddMinutes(5) > TokenValidTo)
44+
{
45+
GetNewToken();
46+
}
47+
return base.CreateRequest(uri, method);
48+
}
49+
50+
private void GetNewToken()
51+
{
52+
AccessToken = TokenProvider.GetOAuthToken(Username, Password);
53+
TokenValidTo = DateTime.UtcNow.AddHours(1);
54+
}
55+
}
56+
57+
}

RedditSharp/Reddit.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ public Reddit(DefaultWebAgent.RateLimitMode limitMode, bool useSsl = true)
115115
DefaultWebAgent.RateLimit = limitMode;
116116
DefaultWebAgent.RootDomain = "www.reddit.com";
117117
}
118-
118+
/// <summary>
119+
/// DEPRECATED: Avoid use as Reddit will be removing this option eventually
120+
/// </summary>
121+
/// <param name="username"></param>
122+
/// <param name="password"></param>
123+
/// <param name="useSsl"></param>
119124
public Reddit(string username, string password, bool useSsl = true)
120125
: this(useSsl)
121126
{
@@ -161,7 +166,7 @@ public Reddit(IWebAgent agent, bool initUser)
161166
}
162167

163168
/// <summary>
164-
/// Logs in the current Reddit instance.
169+
/// Logs in the current Reddit instance. DEPRECATED
165170
/// </summary>
166171
/// <param name="username">The username of the user to log on to.</param>
167172
/// <param name="password">The password of the user to log on to.</param>

RedditSharp/RedditSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Reference Include="System.Xml" />
5252
</ItemGroup>
5353
<ItemGroup>
54+
<Compile Include="BotWebAgent.cs" />
5455
<Compile Include="Moderator Actions\ModActionType.cs" />
5556
<Compile Include="Things\Contributor.cs" />
5657
<Compile Include="Things\ModAction.cs" />

RedditSharp/WebAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public enum RateLimitMode
6262
public static string RootDomain { get; set; }
6363

6464
/// <summary>
65-
/// Used to make calls against Reddit's API using OAuth23
65+
/// Used to make calls against Reddit's API using OAuth2
6666
/// </summary>
6767
public string AccessToken { get; set; }
6868

@@ -239,7 +239,7 @@ public virtual HttpWebRequest CreateRequest(string url, string method)
239239
request.Headers.Set("Authorization", "bearer " + AccessToken);//Must be included in OAuth calls
240240
}
241241
request.Method = method;
242-
request.UserAgent = UserAgent + " - with RedditSharp by /u/sircmpwn";
242+
request.UserAgent = UserAgent + " - with RedditSharp by /u/meepster23";
243243
return request;
244244
}
245245

@@ -258,7 +258,7 @@ protected virtual HttpWebRequest CreateRequest(Uri uri, string method)
258258
request.Headers.Set("Authorization", "bearer " + AccessToken);//Must be included in OAuth calls
259259
}
260260
request.Method = method;
261-
request.UserAgent = UserAgent + " - with RedditSharp by /u/sircmpwn";
261+
request.UserAgent = UserAgent + " - with RedditSharp by /u/meepster23";
262262
return request;
263263
}
264264

0 commit comments

Comments
 (0)