Search code examples
javapythonredditpraw

Getting bad response using JRAW


I am trying to read data from reddit using java. I am using JRAW.

Here is my code:

public class Main {
    public static void main(String args[]) {
        System.out.println('a');
        String username = "dummyName";
        UserAgent userAgent = new UserAgent("crawl", "com.example.crawl", "v0.1", username);
        Credentials credentials = Credentials.script(username, <password>,<clientID>, <client-secret>);
        NetworkAdapter adapter = new OkHttpNetworkAdapter(userAgent);
        RedditClient reddit = OAuthHelper.automatic(adapter, credentials);
        Account me = reddit.me().about();
        System.out.println(me.getName());
        SubmissionReference submission = reddit.submission("https://www.reddit.com/r/diabetes/comments/9rlkdm/shady_insurance_work_around_to_pay_for_my_dexcom/");
        RootCommentNode rcn = submission.comments();
        System.out.println(rcn.getDepth());
        System.out.println();
//        Submission submission1 = submission.inspect();
//        System.out.println(submission1.getSelfText());
//        System.out.println(submission1.getUrl());
//        System.out.println(submission1.getTitle());
//        System.out.println(submission1.getAuthor());
//        System.out.println(submission1.getCreated());
        System.out.println("-----------------------------------------------------------------");
    }
}

I am making two requests as of now, first one is reddit.me().about(); and the second is reddit.submission("https://www.reddit.com/r/diabetes/comments/9rlkdm/ shady_insurance_work_around_to_pay_for_my_dexcom/");

The output is:

a
[1 ->] GET https://oauth.reddit.com/api/v1/me?raw_json=1
[<- 1] 200 application/json: '{"is_employee": false, "seen_layout_switch": true, "has_visited_new_profile": false, "pref_no_profanity": true, "has_external_account": false, "pref_geopopular": "GL(...)
dummyName
[2 ->] GET https://oauth.reddit.com/comments/https%3A%2F%2Fwww.reddit.com%2Fr%2Fdiabetes%2Fcomments%2F9rlkdm%2Fshady_insurance_work_around_to_pay_for_my_dexcom%2F?sort=confidence&sr_detail=false&(...)
[<- 2] 400 application/json: '{"message": "Bad Request", "error": 400}'
Exception in thread "main" net.dean.jraw.ApiException: API returned error: 400 (Bad Request), relevant parameters: []
    at net.dean.jraw.models.internal.ObjectBasedApiExceptionStub.create(ObjectBasedApiExceptionStub.java:57)
    at net.dean.jraw.models.internal.ObjectBasedApiExceptionStub.create(ObjectBasedApiExceptionStub.java:33)
    at net.dean.jraw.RedditClient.request(RedditClient.kt:186)
    at net.dean.jraw.RedditClient.request(RedditClient.kt:219)
    at net.dean.jraw.RedditClient.request(RedditClient.kt:255)
    at net.dean.jraw.references.SubmissionReference.comments(SubmissionReference.kt:50)
    at net.dean.jraw.references.SubmissionReference.comments(SubmissionReference.kt:28)
    at Main.main(Main.java:36)
Caused by: net.dean.jraw.http.NetworkException: HTTP request created unsuccessful response: GET https://oauth.reddit.com/comments/https%3A%2F%2Fwww.reddit.com%2Fr%2Fdiabetes%2Fcomments%2F9rlkdm%2Fshady_insurance_work_around_to_pay_for_my_dexcom%2F?sort=confidence&sr_detail=false&raw_json=1 -> 400
    ... 6 more

As it can been that my first request gives me a response of my username but in the second response i am getting a bad request 400 error.

To check whether my client ID and client secret were working correctly I did the same request using python PRAW library.

import praw
from praw.models import MoreComments

reddit = praw.Reddit(client_id=<same-as-in-java>, client_secret=<same-as-in-java>,
                    password=<same-as-in-java>, user_agent='crawl',
                    username="dummyName")


submission = reddit.submission(
    url='https://www.reddit.com/r/redditdev/comments/1x70wl/how_to_get_all_replies_to_a_comment/')

print(submission.selftext)
print(submission.url)
print(submission.title)
print(submission.author)
print(submission.created_utc)
print('-----------------------------------------------------------------')

This gives the desired result without any errors so the client secret details must be working.

The only doubt I have is in the user agent creation in java UserAgent userAgent = new UserAgent("crawl", "com.example.crawl", "v0.1", username);.

I followed the following link.

What exactly does the target platform, the unique ID or the version mean. I tried to keep the same format as in the link. Also using the same username as in other places. On the other hand the user_agent in python was a string crawl.

Please tell me if I am missing anything and what could be the issue.

Thank you

P.S. I want to do this in java. not python.


Solution

  • Since your first query is working the credentials are correct. In JRAW don't give the whole URL but only the id in the submission function.

    Change this

    SubmissionReference submission = reddit.submission("https://www.reddit.com/r/diabetes/comments/9rlkdm/shady_insurance_work_around_to_pay_for_my_dexcom/");
    

    to this

    SubmissionReference submission = reddit.submission("9rlkdm");
    

    where the id is the random string after /comment/ in the URL.

    Hope this helps.