15

I couldn't get any help on reddit/r/redditdev so I'm hoping you fine folks at stackoverflow can help

I'm trying to call /api/submit.

  1. I can successfully log the user in and retrieve the bearer/access token. (I use passport-reddit node module for this)
  2. I can successfully call /api/needs_captcha
  3. When /needs_captcha returns true, I can successfully call /api/new_captcha
  4. I can display captcha image to the user
  5. Now I try to call submit using nodejs' request module like so

    var options = {
      url: 'https://oauth.reddit.com/api/submit',
      method: 'POST',
      headers: {
          'Authorization': 'bearer '+usertoken
        , 'user-agent': 'node.js'
      },
      json: data
    } 
    request(options, function(error, response, body) ...
    

A sample of my "data" variable looks like this:

{ api_type: 'json',
  kind: 'self',
  sr: 'test',
  title: 'more test',
  text: 'hello world',
  iden: 'o6NsDh4IMCDb2To2DeUXJAgEPkB4O7uS',
  captcha: 'WZRTBL' }

But I get this back:

{
  "jquery":[
    [0,  1,  "call", ["body"]],
    [1,  2,  "attr", "find"],
    [2,  3,  "call", [".status"]],
    [3,  4,  "attr", "hide"],
    [4,  5,  "call", []],
    [5,  6,  "attr", "html"],
    [6,  7,  "call", [""]],
    [7,  8,  "attr", "end"],
    [8,  9,  "call", []],
    [1,  10, "attr", "captcha"],
    [10, 11, "call", ["1oWOOhcicpcpdwjENw5KrR2xHRl7J0aS"]],
    [1,  12, "attr", "find"],
    [12, 13, "call", [".error.BAD_CAPTCHA.field-captcha"]],
    [13, 14, "attr", "show"],
    [14, 15, "call", []],
    [15, 16, "attr", "text"],
    [16, 17, "call", ["care to try these again?"]],
    [17, 18, "attr", "end"],
    [18, 19, "call", []]
  ]
}

Also, I tried running this as a different user, one that has enough karma points and didn't require the captcha.

After I posted, I got this response which seems to indicate success:

{
  "jquery":[
    [0, 1, "call", ["body"]],
    [1, 2, "attr", "find"],
    [2, 3, "call", [".status"]],
    [3, 4, "attr", "hide"],
    [4, 5, "call", []],
    [5, 6, "attr", "html"],
    [6, 7, "call", [""]],
    [7, 8, "attr", "end"],
    [8, 9, "call", []]
  ]
}

But I can't find my posted message in the /r/test subreddit.

I must be doing something wrong entirely...

Andy
  • 17,423
  • 9
  • 52
  • 69
kane
  • 5,465
  • 6
  • 44
  • 72
  • For reference, [here](https://www.reddit.com/dev/api#POST_api_submit) is the documentation for that endpoint. – Xiong Chiamiov Jul 14 '15 at 18:47
  • 1
    Does it look any different if you set `extension` to "json"? – Xiong Chiamiov Jul 14 '15 at 18:48
  • Also, [here](https://www.reddit.com/r/redditdev/comments/3d36jl/getting_errorbad_captchafieldcaptcha_on_apisubmit/) is the /r/redditdev thread. – Xiong Chiamiov Jul 14 '15 at 18:51
  • I'm using that doc reference but I wish it was more descriptive, perhaps with examples. I posted this question on /r/redditdev already a few days ago and couldn't get any answers. I haven't tried extension: "json" but I can. Why do you think that'll help? – kane Jul 14 '15 at 18:55
  • @kane http://www.reddit.com/r/rawjs/wiki/documentation – stdob-- Jul 21 '15 at 07:28
  • @stdob thanks. if all else fails, I'll try this. I really would like to understand what I'm doing wrong though since this is more of a learning exercise than a project that needs to get done – kane Jul 21 '15 at 16:39
  • What about posting to `https://oauth.reddit.com/api/submit.json`? – David Ehrmann Jul 21 '15 at 23:09

3 Answers3

6

After going through different documentation related to /api/submit, I have discovered that you have missed the field uh of your sample of "data". Missing of uh entails to errors that look like yours (look documentation below)

Here more documentation related what I said:

https://github.com/reddit/reddit/wiki/API:-submit http://www.reddit.com/dev/api

juanmajmjr
  • 1,055
  • 7
  • 11
  • 3
    The doc also says that "uh" is not required when logging in with OAuth, which I am doing. Also, my error message does not contain "please login to do that" so it's not the same error – kane Jul 22 '15 at 17:30
  • I will try to find more information about the problem. – juanmajmjr Jul 22 '15 at 17:46
0

Try changing your User-Agent header to something more descriptive. See the guidelines here: https://github.com/reddit/reddit/wiki/API. I've heard of people having issues just using a generic user agent string like node.js.

If you are still having trouble, the Apigee console (https://apigee.com/console/reddit) has helped me debug issues with the reddit API. Just add your User-Agent header and the Authorization token and mess with different parameters through the console, if that's faster than building changes to your application.

Damien Diehl
  • 383
  • 4
  • 13
0

I've just had a similar problem, and the fix (to the your second error) was to not send as JSON but as a query in the URI.

/api/submit?api_type=json&kind=self&sr=test&title=more%20test&text=hello%20world

Append any other fields as needed.

Keep the 'url', 'method', and 'headers' that you used.

That should then return a json like so:

"json": {
    "data": {
        "id": "12abcd",
        "name": "t3_12abcd",
        "url": "https://www.reddit.com/r/test/comments/12abcd/more_test/"
    },
    "errors": []
}