I've automating message sending in discord with a simple script in python
def sendMsg(token:str, channel:str, message:str):
url = 'https://discord.com/api/v9/channels/' + channel + '/messages'
data = {"content": message}
header = {"authorization": token}
r = requests.post(url, data=data, headers=header)
print(r.status_code)
but I want to automate also sending slash commands and there is were i get the error, i've tryed using the url url = 'https://discord.com/api/v9/interactions' maybe the error is in the url that need a channel id but looking with dev tools in the headers: Request URL: https://discord.com/api/v9/interactions the json im going to send gives some errors like true is undefined did you mean True same as null is undefined, i've just doble quoted that, do you know what is the error? the status code is always 400
full code:
import requests
import json
token = 'thIsIsMySecreTt0k3n'
channelId = '1015655803383988314'
with open('data_json.json','r') as f:
s = f.read()
print(s)
dta = s
f.close()
def sendMsg(token:str, channel:str, message:str):
url = 'https://discord.com/api/v9/channels/' + channel + '/messages'
data = {"content": message}
#data = dta
header = {"authorization": token}
r = requests.post(url, data=data, headers=header)
print(r.status_code)
def sendRank(token:str,channel:str):
url = 'https://discord.com/api/v9/interactions'
data = dta
header = {"authorization": token}
r = requests.post(url, data=data, headers=header)
print(r.status_code)
print(type(eval(dta)))
sendRank(token, channelId)
you may also need to see the data_json.json that try to send /rank to probot
{"type":2,"application_id":"282859044593598464","guild_id":"1003270895185436803","channel_id":"1003270896359845998","session_id":"0eaab6fb154d359c5665ca016f1eec11","data":{"version":"971443831096635452","id":"971443830870126634","name":"rank","type":1,"options":[],"application_command":{"id":"971443830870126634","application_id":"282859044593598464","version":"971443831096635452","default_permission":"true","default_member_permissions":"null","type":1,"name":"rank","description":"View your rank card or someone else's in the server.","dm_permission":"true","options":[{"type":6,"name":"user","description":"User to get rank of."}]}}}
or the raw json without beeing modified
{"type":2,"application_id":"282859044593598464","guild_id":"1003270895185436803","channel_id":"1015655803383988314","session_id":"0eaab6fb154d359c5665ca016f1eec11","data":{"version":"971443831096635452","id":"971443830870126634","name":"rank","type":1,"options":[],"application_command":{"id":"971443830870126634","application_id":"282859044593598464","version":"971443831096635452","default_permission":true,"default_member_permissions":null,"type":1,"name":"rank","description":"View your rank card or someone else's in the server.","dm_permission":true,"options":[{"type":6,"name":"user","description":"User to get rank of."}]},"attachments":[]},"nonce":"1015670347103469568"}
Change data=data to json=data, you should get a 204 response code which means it worked.
You also do not need all of that data, the only ones you need are below
{"type":2,"application_id":"","guild_id":"","channel_id":"","session_id":"","data":{"version":"","id":"","name":""}}
Session id does not have to be a vaild id, it just has to be a string that isn't empty.
Related
import requests
import json
# Define the API endpoint and necessary headers and parameters
url = "https://www.googleapis.com/customsearch/v1"
# url = "https://yande.re/post/similar"
api_key = "replace with your api"
headers = {"Content-Type": "image/jpeg"}
params = {"type": "similar", "cx": "d47119ff66e004d6b", "key":api_key}
# Read the image file and convert it to binary
with open("C:\\Users\\Administrator\\Downloads\\theme\\3f559715-f407-47de-8540-1cd9e4fdc56c.jpg", "rb") as f:
image_data = f.read()
# Send the POST request
response = requests.post(url, headers=headers, params=params, data=image_data)
# Check the status code of the response
if response.status_code == 200:
print('ok')
# Parse the JSON response
data = json.loads(response.text)
# Extract the relevant data from the response
similar_images = data["similar_images"]
else:
print(response.status_code)
print('no')
what wrong with my code? My url was wrong? i alway get no when run it
when I try with another URL like https://yande.re/post/similar i get return. what was happen?
https://www.googleapis.com/customsearch/v1 is a GET Request API. But you are trying to do POST method on this API.
Here is the sample of GET Request for this API
url = "https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={SEARCH_ENGINE_ID}&q={query}&start={start}"
# make the API request
data = requests.get(url).json()
I am trying to create dm
import requests
import json
url = 'https://discordapp.com/api/v6/users/#me/channels'
token = 'token'
body = {
'recipient_id': '123456789'
}
data = {'data':json.dumps(body)}
headers = {
"Authorization": token,
"Content-Type":"application/json"
}
r = requests.post(url, data=data, headers=headers)
print(r.content)
print(r.text)
This code receives response with "400: Bad request" message.
I've read this but I can't find any examples of correct usage of this request.
I finally understood my mistake, I shouldn't pass dictionary as data but should pass a str, so I changed data=data to data=json.dumps(data) and it worked
I am having a problem with authentication error from Alfresco, maybe somebody could enlighten me while im trying to find log files from the mess that is Alfresco.
curl -uadmin:password -X POST https://hostname/alfresco/api/-default-/public/alfresco/versions/1/nodes/node_uuid/children -F filedata=#/home/user/file.pdf
Now when I am trying to use python requests(TM) it gives me a 401. Ive checked the requests and they both send the same Authentication hash
This is the python code:
def handle_row_for_request(row):
url = 'https://' + hostname + '/alfresco/api/-default-/public/alfresco/versions/1/nodes/' + base_uuid + '/children'
data = '/home/user/file.pdf'
with open(data, 'rb') as payload:
headers={"content-type": "multipart/form-data"}
response = requests.post(url, headers=headers, data=payload, auth=(username, password))
You are using the data argument, but you need to use the file argument:
def handle_row_for_request(row):
url = 'https://' + hostname + '/alfresco/api/-default-/public/alfresco/versions/1/nodes/' + base_uuid + '/children'
files = {'filedata': ('/home/user/file.pdf', open('/home/user/file.pdf', 'rb'))}
headers={"content-type": "multipart/form-data"}
response = requests.post(url, headers=headers, files=files, auth=(username, password))
Below is the Python code which works fine for the upload which also uses the basic auth. Please check the request params.
import json
import requests
url = "http://localhost:8080/alfresco/service/api/upload"
auth = ("admin", "admin")
files = {"filedata": open("/tmp/foo.txt", "rb")}
data = {"siteid": "test", "containerid": "documentLibrary"}
r = requests.post(url, files=files, data=data, auth=auth)
print(r.status_code)
print(json.loads(r.text))
There were 2 parts to the problem. First I needed to use the file argument instead of data. On top of that Alfresto wants to get 'filedata' string inside the file argument.
Secondly auth(user,pw) generated a hash that had a single = sign in the end and alfresco said 401. When I used cURL and got the password hash from there it had two = signs in the end. Weird right? But that worked. Instead of using auth(user,pw) I set the 'Authorization': 'Basic ${base64thing}' as the authentication.
response = requests.post(url, headers={'Authorization': 'Basic base64string'}, files={'filedata': open(/path/, 'rb')})```
I would really appreciate some help here. I am trying to use the Spotify API to add albums to a users library. I have been struggling with a Malformed Json Payload, and am completely out of ideas.
Here is a simplified version of what I am currently sitting on
url = 'https://api.spotify.com/v1/me/albums'
payload = {'body': ['01kTgTBiZkCFY3ZH2hBH6u', '4sz6Fn4BYORRLIc1AvQwQx']}
headers = {'Authorization':'Bearer {}'.format(access_token), 'Content-Type':'application/json',}
response = requests.put(url,headers=headers, data=payload)
print(response.json())
The error I am receiving is in the json response:
{'error': {'status': 400, 'message': 'Malformed json payload'}}
I have tried changing requests.put as per below, but all attempts are returning the same error
response = requests.put(url,headers=headers, json=payload)
response = requests.put(url,headers=headers, data=json.dumps(payload))
response = requests.put(url,headers=headers, json=json.dumps(payload))
07bYtmE3bPsLB6ZbmmFi8d: This spotify id is for the album, Dancefloor Hits #1. I checked my spotify acct and there it was in my Albums on my acct. Below is my code to run that.
import requests
url = "https://api.spotify.com/v1/me/albums"
payload = {"ids": "27cZdqrQiKt3IT00338dws"}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer %s' % (access_token) # paste your access token in here
}
''' you can use the same syntax as above but the key was that your ID of album had to be a **parameter** not ***data***.
To do that you use the params kwarg'''
response = requests.request("PUT", url, headers=headers, params = payload)
print(response.status_code) # should print out 200 when you run the code
# shows whether status was valid
i'm working on settin up a rest api with python, however i'm having some problem getting it to work.
I'm working with the TV DB rest api: https://api.thetvdb.com/swagger
and using python with Requests library to pull out the information.
My code is currently:
import json
import requests
URL = "https://api.thetvdb.com/"
API_KEY = "Api_key"
USER_KEY = "Key"
USERNAME = "Name"
headers = {"Accept": "application/json"}
params = {
"apikey": API_KEY,
"userkey": USER_KEY,
"username": USERNAME
}
resp = requests.post(URL + "login/", headers = headers ,params=params)
if resp.status_code != 200:
print('error: ' + str(resp.status_code))
else:
print('Success')
So far i'm only getting error code 401, not sure why.
Solved:
2 Things needed to be changed
1. The resp was changed into:
resp = requests.post(URL + "login/", headers = headers, data=json.dumps(params))
The header had to have
"Content-Type": "application/json"
added to it :) It's now working, thanks everyone
The login parameters probably need to be a JSON-encoded string POSTed as the body of the message.
Try resp = requests.post(URL + "login/", headers = headers, data=json.dumps(params))