Python Pastebin api (edited)

702 views Asked by At

(Edited) I just installed PastebinAPI
and got an error urllib has no attribute urlopen/urlencode
I went to the pastebin api file myself and I'm wondering if I should change the line

import urllib

#to
import urllib.request
import urllib.parse

I don't know if that's safe so I'm asking here.
As I said I just installed the api so here's my current code

from pastebin import PastebinAPI

pasteBin = PastebinAPI()
my_key = pasteBin.generate_user_key(my_dev-key, my-username, my-password)
print(my_key)

I found some other useful links like
Request Pastebin Example: Should I just use request instead of the api?
Urllib Pastebin class: Thinking of using this if the api doesn't work. Tho they have the same problem with urlopen and urlencode

Okay I've tried changing the urllib to urllib.request/parse but a new error shows
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.

What does it mean? So confused.
Is the api outdated? If so can someone link me a dated api?

Here's the full error

Traceback (most recent call last):
  File "C:\Users\(My-Name)\PycharmProjects\Project\main.py", line 4, in <module>
    key = pastebin.generate_user_key('(my-key)', '(username)', '(password)')
  File "C:\Users\(My-Name)\PycharmProjects\Project\venv\lib\site-packages\pastebin.py", line 574, in generate_user_key
    request_string = urllib.request.urlopen(self._api_login_url, urllib.parse.urlencode(argv))
  File "C:\Users\(My-Name)\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\(My-Name\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 514, in open
    req = meth(req)
  File "C:\Users\(My-Name)\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1273, in do_request_
    raise TypeError(msg)
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.

1

There are 1 answers

1
Lukas Neumann On

Yes, you should do this when you are working with Python 3.

Based on this question: 'module' has no attribute 'urlencode', try the following:

urllib has been split up in Python 3.

The urllib.urlencode() function is now urllib.parse.urlencode(),

the urllib.urlopen() function is now urllib.request.urlopen().