0
votes

I'm trying to use the pastebin api with docs: python https://pastebin.com/doc_api. Using the urllib library: https://docs.python.org/3/library/urllib.html.

import urllib.request
import urllib.parse

def main():

    def pastebinner():
        site = 'https://pastebin.com/api/api_post.php'
        dev_key = 
        code = "12345678910, test"      
        our_data = urllib.parse.urlencode({"api_dev_key": dev_key, "api_option": "paste", "api_paste_code": code})  
        our_data = our_data.encode()                    
        resp = urllib.request.urlopen(site, our_data)
        print(resp.read())
        
    pastebinner()

if __name__ == "__main__":
    main()

Here's the error i get:

File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 214, in urlopen return opener.open(url, data, timeout) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 523, in open response = meth(req, response) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 632, in http_response response = self.parent.error( File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 561, in error return self._call_chain(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 494, in _call_chain result = func(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 641, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 422: Unprocessable entity

Any ideas regarding the reason for getting this error?

bump: I still have no idea please help. bump2: :v

2
@Nearoo why does that make the request unparseable? api_paste_code should be the content of the paste right? from the docs: Creating A New Paste, [Required Parameters] Include all the following POST parameters when you request the url: 1. api_dev_key - which is your unique API Developers Key. 2. api_option - set as paste, this will indicate you want to create a new paste. 3. api_paste_code - this is the text that will be written inside your paste. Leaving any of these parameters out will result in an error.Jon Doe
Right :) Makes no sense, sorry.Nearoo
@Nearoo There's a more user friendly python library but I really dont want to use it because urllib is part of the standard library. Based on some reading I've done it seems like possible pastebin thinks im a bout and thats the reason why. But what do i put in my code to fix this? I know you may not know but I think that's the question here.Jon Doe

2 Answers

0
votes

You are using urllib.request.urlopen(site, our_data) which is an HTTP GET (default for anything in urllib). You need to do an HTTP POST instead. Obligatory w3 link

Please note that the code below is untested

import urllib.request
import urllib.parse


def main():

    def pastebinner():
        site = 'https://pastebin.com/api/api_post.php'
        dev_key = 'APIKEYGOESHERE'
        code = "12345678910, test"
        our_data = urllib.parse.urlencode({"api_dev_key": dev_key, "api_option": "paste", "api_paste_code": code})
        our_data = our_data.encode()
        request = urllib.request.Request(site, method='POST')
        resp = urllib.request.urlopen(request, our_data)
        print(resp.read())

    pastebinner()


if __name__ == "__main__":
    main()

The error is very unhelpful. I mean, why not return a teapot response instead?

0
votes

leaving this here in case anyone else runs into this issue. Not 100% sure about this, will test later DONT USE URLLIB2 USE httplib2. I believe that will fix your problem.