Skip to content

Authentication

masecla22 edited this page Oct 12, 2021 · 2 revisions

Authentication

To authenticate with Reddit, you are going to need as follows

  • Reddit account username that has been authorized to your app (if you created your app, then you are one by default)
  • Reddit account password
  • Reddit App ID
  • Reddit App Secret

Personal application (with user)

0. TL;DR

Reddit4J client = Reddit4J.rateLimited().setUsername("Username").setPassword("Password")
		.setClientId("App ID").setClientSecret("Secret")
		.setUserAgent(new UserAgentBuilder().appname("App Name").author("Your Name").version("1.0"));
client.connect();

1. Building a user agent

Reddit will heavily restrict any generic User-Agent as they want to know what apps are doing what. Building a user agent from scratch however can be a little tedious, so we added a utility UserAgentBuilder. Here is an example of a user agent being built.

UserAgentBuilder userAgent = new UserAgentBuilder().appname("Reddit4J").author("masecla22").version("1.0");

2. Building your Reddit4J client

This is pretty easy to do and simply calling a list of methods on it will be able to do it

Reddit4J client = Reddit4J.rateLimited().setUsername("Username").setPassword("Password")
		.setClientId("App ID").setClientSecret("Secret");

3. Combining the two, and obtaining a token

Finally, you will need to set the user agent you built and obtain a token using the connect method.

UserAgentBuilder userAgent = new UserAgentBuilder().appname("Reddit4J").author("masecla22").version("1.0");
Reddit4J client = Reddit4J.rateLimited().setUsername("Username").setPassword("Password")
		.setClientId("App ID").setClientSecret("Secret")
		.setUserAgent(userAgent);

client.connect(); // Obtains a token

Userless authentication

0. TL;DR

Reddit4J client = Reddit4J.rateLimited()
		.setClientId("App ID").setClientSecret("Secret")
		.setUserAgent(new UserAgentBuilder().appname("App Name").author("Your Name").version("1.0"));
client.userlessConnect();

1. Building a user agent

Reddit will heavily restrict any generic User-Agent as they want to know what apps are doing what. Building a user agent from scratch however can be a little tedious, so we added a utility UserAgentBuilder. Here is an example of a user agent being built.

UserAgentBuilder userAgent = new UserAgentBuilder().appname("Reddit4J").author("masecla22").version("1.0");

2. Building your Reddit4J client

This is pretty easy to do and simply calling a list of methods on it will be able to do it

Reddit4J client = Reddit4J.rateLimited().setClientId("App ID").setClientSecret("Secret");

3. Combining the two, and obtaining a token

Finally, you will need to set the user agent you built and obtain a token using the connect method.

UserAgentBuilder userAgent = new UserAgentBuilder().appname("Reddit4J").author("masecla22").version("1.0");
Reddit4J client = Reddit4J.rateLimited().setClientId("App ID").setClientSecret("Secret")
		.setUserAgent(userAgent);

client.userlessConnect(); // Obtains a token