Quickstart
Add the Dependency
JRAW is hosted on Bintray's jCenter.
Gradle:
repositories {
jcenter()
}
dependencies {
compile "net.dean.jraw:JRAW:$jrawVersion"
}
Maven:
Add jCenter (https://jcenter.bintray.com
) to your repositories and then add the dependency to the pom.xml
:
<dependency>
<groupId>net.dean.jraw</groupId>
<artifactId>JRAW</artifactId>
<version>${jraw.version}</version>
</dependency>
Choose a User-Agent
The first step in using the reddit API effectively is making sure you're sending a descriptive User-Agent header. This allows reddit to block buggy versions of your app while not affecting others. An effective User-Agent consists of four parts:
- The target platform
- A unique ID (usually your package)
- A version
- A reddit username.
A UserAgent for a super useful bot could look like this:
UserAgent userAgent = new UserAgent("bot", "com.example.usefulbot", "v0.1", "mattbdean");
Create a reddit OAuth2 app
reddit uses OAuth2 to authenticate 3rd party apps. The first thing you'll need to do is to register your app here. For the sake of simplicity, let's create a script app.
You'll need the client ID and client secret later.
Authenticate
Let's tell JRAW to authenticate our client:
// Create our credentials
Credentials credentials = Credentials.script("<username>", "<password>",
"<client ID>", "<client secret>");
// This is what really sends HTTP requests
NetworkAdapter adapter = new OkHttpNetworkAdapter(userAgent);
// Authenticate and get a RedditClient instance
RedditClient reddit = OAuthHelper.automatic(adapter, credentials);
Here <username>
and <password>
must be for the account that created the script.
See the OAuth2 page page for more.