Fermin Pitol
Fermin Pitol

Reputation: 486

Unable to submit a post with PRAW API

I have this code:

import praw

print('starting')
reddit = praw.Reddit(client_id='****',
                     client_secret='********',
                     user_agent='****',
                     username = '****',
                     password = '****')

r = reddit.post("/api/submit",data={'title':'my firts title','text':'the text of my post','sr':'r/test'})

print("finishing")

But it returns with the error:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-19-7e66ffa81635> in <module>
      9                      password = '*****')
     10 
---> 11 r = reddit.post("/api/submit",data={'title':'my firts title','text':'the text of my post','sr':'r/test'})
     12 
     13 print("finishing")

~\AppData\Local\Continuum\anaconda3\lib\site-packages\praw\reddit.py in post(self, path, data, files, params)
    481         data = self.request('POST', path, data=data or {}, files=files,
    482                             params=params)
--> 483         return self._objector.objectify(data)
    484 
    485     def put(self, path, data=None):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\praw\objector.py in objectify(self, data)
    148             if len(errors) == 1:
    149                 raise APIException(*errors[0])
--> 150             assert not errors
    151 
    152         elif isinstance(data, dict):

AssertionError: 

and in some occasions the same code returns :

---------------------------------------------------------------------------
APIException                              Traceback (most recent call last)
<ipython-input-27-b62f9f5f585d> in <module>
      9                      password = '****')
     10 
---> 11 r = reddit.post("/api/submit",data={'title':'my firts title','text':'the text of my post','sr':'r/test'})
     12 
     13 print("finishing")

~\AppData\Local\Continuum\anaconda3\lib\site-packages\praw\reddit.py in post(self, path, data, files, params)
    481         data = self.request('POST', path, data=data or {}, files=files,
    482                             params=params)
--> 483         return self._objector.objectify(data)
    484 
    485     def put(self, path, data=None):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\praw\objector.py in objectify(self, data)
    147             errors = data['json']['errors']
    148             if len(errors) == 1:
--> 149                 raise APIException(*errors[0])
    150             assert not errors
    151 

APIException: INVALID_OPTION: 'opci\xf3n inv\xe1lida' on field 'sr'

To be honest I do not know what I am doing wrong. I suppose there is a better way to simple submit a post in reddit, but the documentation is not so helpful

Upvotes: 0

Views: 657

Answers (1)

Nick Vitha
Nick Vitha

Reputation: 476

You should do:

my_post = reddit.subreddit('subreddit').submit('My Title', selftext='Stuff you want to put in the textbox')

Note that subreddit shouldn't include the r/.

as per: https://praw.readthedocs.io/en/latest/code_overview/models/subreddit.html#praw.models.Subreddit.submit

Upvotes: 2

Related Questions