简体   繁体   中英

Python 3.5 / Pastebin "Bad API request, invalid api_option"

I'm working on a twitch irc bot and one of the components I wanted to have available was the ability for the bot to save quotes to a pastebin paste on close, and then retrieve the same quotes on start up.

I've started with the saving part, and have hit a road block where I can't seem to get a valid post, and I can't figure out a method.

#!/usr/bin/env python3

import urllib.parse
import urllib.request

# --------------------------------------------- Pastebin Requisites --------------------------------------------------

pastebin_key = 'my pastebin key'  # developer api key, required. GET: http://pastebin.com/api
pastebin_password = 'password'  # password for pastebin_username
pastebin_postexp = 'N'  # N = never expire
pastebin_private = 0  # 0 = Public 1 = unlisted 2 = Private
pastebin_url = 'http://pastebin.com/api/api_post.php'
pastebin_username = 'username'  # user corresponding with key


# --------------------------------------------- Value clean up --------------------------------------------------

pastebin_password = urllib.parse.quote(pastebin_password, safe='/')
pastebin_username = urllib.parse.quote(pastebin_username, safe='/')

# --------------------------------------------- Pastebin Functions --------------------------------------------------

def post(title, content):  # used for posting a new paste
    pastebin_vars = {'api_option': 'paste', 'api_user_key':     pastebin_username, 'api_paste_private': pastebin_private,
                 'api_paste_name': title, 'api_paste_expire_date': pastebin_postexp,  'api_dev_key': pastebin_key,
                 'api_user_password': pastebin_password, 'api_paste_code': content}
    try:
        str_to_paste = ', '.join("{!s}={!r}".format(key, val) for (key, val) in pastebin_vars.items())  # dict to str :D
        str_to_paste = str_to_paste.replace(":", "")  # remove :
        str_to_paste = str_to_paste.replace("'", "")  # remove '
        str_to_paste = str_to_paste.replace(")", "")  # remove )
        str_to_paste = str_to_paste.replace(", ", "&")  # replace dividers with &
        urllib.request.urlopen(pastebin_url, urllib.parse.urlencode(pastebin_vars)).read()
        print('did that work?')
    except:
        print("post submit failed :(")
        print(pastebin_url + "?" + str_to_paste)  # print the output for test

 post("test", "stuff")

I'm open to importing more libraries and stuff, not really sure what I'm doing wrong after working on this for two days straight :S

import urllib.parse
import urllib.request

PASTEBIN_KEY = 'xxx'
PASTEBIN_URL = 'https://pastebin.com/api/api_post.php'
PASTEBIN_LOGIN_URL = 'https://pastebin.com/api/api_login.php'
PASTEBIN_LOGIN = 'my_login_name'
PASTEBIN_PWD = 'yyy'

def pastebin_post(title, content):
    login_params = dict(
        api_dev_key=PASTEBIN_KEY,
        api_user_name=PASTEBIN_LOGIN,
        api_user_password=PASTEBIN_PWD
    )

    data = urllib.parse.urlencode(login_params).encode("utf-8")
    req = urllib.request.Request(PASTEBIN_LOGIN_URL, data)

    with urllib.request.urlopen(req) as response:
        pastebin_vars = dict(
            api_option='paste',
            api_dev_key=PASTEBIN_KEY,
            api_user_key=response.read(),
            api_paste_name=title,
            api_paste_code=content,
            api_paste_private=2,
        )
        return urllib.request.urlopen(PASTEBIN_URL, urllib.parse.urlencode(pastebin_vars).encode('utf8')).read()

rv = pastebin_post("This is my title", "These are the contents I'm posting")
print(rv)

Combining two different answers above gave me this working solution.

I didn't know about pastebin until now. I read their api and tried it for the first time, and it worked perfectly fine.

Here's what I did:

  • I logged in to fetch the api_user_key .
  • Included that in the posting along with api_dev_key .
  • Checked the website, and the post was there.

Here's the code:

import urllib.parse
import urllib.request


def post(url, params):
    data = urllib.parse.urlencode(login_params).encode("utf-8")
    req = urllib.request.Request(login_url, data)
    with urllib.request.urlopen(req) as response:
       return response.read()


# Logging in to fetch api_user_key
login_url = "http://pastebin.com/api/api_login.php"
login_params = {"api_dev_key": "<the dev key they gave you",
                "api_user_name": "<username goes here>",
                "api_user_password": "<password goes here>"}
api_user_key = post(login_url, login_params)


# Posting some random text
post_url = "http://pastebin.com/api/api_post.php"
post_params = {"api_dev_key": "<the dev key they gave you",
               "api_option": "paste",
               "api_paste_code": "<head>Testing</head>",
               "api_paste_private": "0",
               "api_paste_name": "testing.html",
               "api_paste_expire_date": "10M",
               "api_paste_format": "html5",
               "api_user_key": api_user_key}
response = post(post_url, post_params)

Only the first three parameters are needed for posting something, the rest are optional.

First, your try / except block is throwing away the actual error. You should almost never use a "bare" except clause without capturing or re-raising the original exception. See this article for a full explanation.

Once you remove the try / except , and you will see the underlying error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "paste.py", line 42, in post
    urllib.request.urlopen(pastebin_url, urllib.parse.urlencode(pastebin_vars)).read()
  File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 461, in open
    req = meth(req)
  File "/usr/lib/python3.4/urllib/request.py", line 1112, in do_request_
    raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.

This means you're trying to pass a unicode string into a function that's expecting bytes. When you do I/O (like reading/writing files on disk, or sending/receiving data over HTTP) you typically need to encode any unicode strings as bytes. See this presentation for a good explanation of unicode vs. bytes and when you need to encode and decode.

Next, this line:

urllib.request.urlopen(pastebin_url, urllib.parse.urlencode(pastebin_vars)).read()

Is throwing away the response, so you have no way of knowing the result of your API call. Assign this to a variable or return it from your function so you can then inspect the value. It will either be a URL to the paste, or an error message from the API.

Next, I think your code is sending a lot of unnecessary parameters to the API and your str_to_paste statements aren't necessary.

I was able to make a paste using the following, much simpler, code:

import urllib.parse
import urllib.request

PASTEBIN_KEY = 'my-api-key' # developer api key, required. GET: http://pastebin.com/api
PASTEBIN_URL = 'http://pastebin.com/api/api_post.php'

def post(title, content):  # used for posting a new paste
    pastebin_vars = dict(
        api_option='paste',
        api_dev_key=PASTEBIN_KEY,
        api_paste_name=title,
        api_paste_code=content,
    )
    return urllib.request.urlopen(PASTEBIN_URL, urllib.parse.urlencode(pastebin_vars).encode('utf8')).read()

Here it is in use:

>>> post("test", "hello\nworld.")
b'http://pastebin.com/v8jCkHDB'

在撰写本文时,API 似乎不接受 http 请求,因此请确保 URL 的格式为https://pas...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM