-
Notifications
You must be signed in to change notification settings - Fork 128
Quickstart
This is for version 0.9.0 and below. The documentation for v1.0.0+ can be found here
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>
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 script named "Awesome Script" could look like this:
UserAgent myUserAgent = UserAgent.of("desktop", "net.dean.awesomescript", "v0.1", "thatJavaNerd");
Now we can create a RedditClient
:
RedditClient redditClient = new RedditClient(myUserAgent);
Because reddit uses OAuth2, this RedditClient will need to be authenticated before it can do anything useful. Fortunately, RedditClient
comes with a helper class: OAuthHelper
. This guide will assume you picked a script app, since those are the easiest to authenticate. If you need to use a web or installed app, see the OAuth2 page.
The first thing we need to do is get our credentials in order. JRAW comes with a Credentials
class to help us organize everything.
Credentials credentials = Credentials.script("<username>", "<password>", "<clientId>", "<clientSecret>");
To use this data, we need to use the RedditClient
's OAuthHelper
. Since the Credentials
we created was for a script, we can make use of OAuthHelper
's easyAuth(Credentials)
method.
OAuthData authData = redditClient.getOAuthHelper().easyAuth(credentials);
Finally, we can notify the RedditClient
that we have been authorized.
redditClient.authenticate(authData);
Now we are fully authorized and are able to make requests to the API successfully! To test it out, try redditClient.me()
.
Note that you will need to renew your access token after one hour. To do this, see the OAuth2 page.