简体   繁体   中英

"Bad API request, invalid api_dev_key" when POSTing to pastebin API using Python 3.9 (win10)

I have been attempting to solve this far too longer than id like to admit, I think the problem is how the data is being parsed with json and being interoperated via the API, as I do not have the same issue with the first function, but run into it with the second. Any help will be great.

import urllib, requests, json

def generateUserKey(username, password):
    global devKey
    return urllib.request.urlopen("https://pastebin.com/api/api_login.php",
                                  urllib.parse.urlencode({"api_dev_key": devKey, "api_user_name": username, "api_user_password": password}).encode()).read()

def paste(userKey, text):
    global devKey
    datA = json.dumps({"api_dev_key": devKey, "api_paste_code": text, "api_user_key": userKey, "api_paste_name": "lol", "api_paste_format": "none", "api_paste_private": int(1), "api_paste_expire_date": "10M" })
    resp = requests.post(url="https://pastebin.com/api/api_post.php", json=datA, data=datA)
    print(resp.text)

 key = generateUserKey(devKey, userName, passWord)
 print(key)
 paste(key, testString)

when ran I generate the following:

c0ce26a1c46d5fff3a254e519003ebb0
Bad API request, invalid api_dev_key
None

the dev key isnt invalid as its being used in the previous function to login and obtain a session key, so this is where I am stuck. Any help?

this could help:

import requests  # see https://2.python-requests.org/en/master/
import json
def generateUserKey(data):
    login = requests.post("https://pastebin.com/api/api_login.php", data=data)
    print("Login status: ", login.status_code if login.status_code != 200 else "OK/200")
    print("User token: ", login.text)
    return login.text
    
def paste(data):
    r = requests.post("https://pastebin.com/api/api_post.php", data)
    print("Paste send: ", r.status_code if r.status_code != 200 else "OK/200")
    print("Paste URL: ", r.text)

key = 'your key'
text = "hi"
t_title = "title of paste"

login_data = {
    'api_dev_key': key,
    'api_user_name': 'username',
    'api_user_password': 'password'
}

data = {
    'api_option': 'paste',
    'api_dev_key': key,
    'api_paste_code': text,
    'api_paste_name': t_title,
    'api_user_key': None,
    # 'api_paste_expire_date': 'see_https://pastebin.com/api', # optional
    # 'api_paste_private': 1, # 0 = public, 1 = unlisted, 2 = private
    'api_user_key': generateUserKey(login_data)
    # see_https://pastebin.com/api fro all the other arguments you can add
}
# if your data is already in json format, you can use json.dumps(data)
# data = json.dumps(data) # and now its a dict, and it can be feed to the requests.post
paste(data)

if you have any questions don't hesitate to ask

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