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.
I'm trying to get a token using Spotify's Client Credentials Flow and Python, however I just get the following:
{"error":"invalid_client","error_description":"Invalid client"}
I'm following this guide - https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/
Using this example script as a starting point - https://www.w3schools.com/python/showpython.asp?filename=demo_requests_post_headers
Here's my code (I've changed the Base 64 encoded string that contains the client ID and client secret key):
import requests
url = 'https://accounts.spotify.com/api/token'
myobj = {'grant_type': 'client_credentials'}
#use the 'headers' parameter to set the HTTP headers:
x = requests.post(url, data = myobj, headers = {"Authorization": "Basic Base64EncodedStringHere==","Content-Type": "application/x-www-form-urlencoded"})
print(x.text)
If I change the last line to print(x), I just get: <Response [400]>
Clearly I'm doing something wrong, but I can't figure out what?
I had the same problem while following a tutorial but I manage to find a solution on the Spotify community.
import requests
import base64
client_id = "your client id here"
client_secret = "your client secret here"
encoded = base64.b64encode((client_id + ":" + client_secret).encode("ascii")).decode("ascii")
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + encoded
}
payload = {
"grant_type": "client_credentials"
}
response = requests.post("https://accounts.spotify.com/api/token", data=payload, headers=headers)
print(response.text)
I am following a simple example (or what I thought was simple) of creating a python script that used the REST api to connect to wordpress.
However, I am getting a 403 error. My credentials are correct because I can log in with them.
I have been working over this for awhile now. Can anyone see where my error might be? Thank you.
url = "https://prod-wp.xxxxxx.com/wp-json/wp/v2/posts"
user = "xxxxxx"
password = "xxxxxxxx"
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
header = {'Authorization': 'Basic ' + token.decode('utf-8')}
response = requests.get(url , headers=header)
print(response)
<Response [403]>
EDIT
I have changed the code and this seems to work.
import requests
import os
from dotenv import load_dotenv
load_dotenv()
BASE_URL = 'https://website.com/wp-json'
WP_URL = os.getenv("WP_URL")
WP_USERNAME = os.getenv("WP_USERNAME")
WP_PASSWORD = os.getenv("WP_PASSWORD")
def get_headers():
wp_credentials = {'username': WP_USERNAME, 'password': WP_PASSWORD}
jwt_response = requests.post(f'{BASE_URL}/jwt-auth/v1/token', json=wp_credentials)
jwt_token = jwt_response.json()['token']
headers = {
"Authorization": "Bearer %s" % jwt_token,
"Content-Type": "application/json",
"Accept": "application/json",
}
print(f'Headers are equal to: {headers}')
return headers
get_headers()
Looking to connect to the Gooddata API and export a report via the API in python. The documentation is a bit confusing to follow.
I've defined a login to my instance of gooddata:
from urllib2 import Request, urlopen
import json
import requests
def login_gooddata(my_email, my_password):
url = 'https://secure.gooddata.com/gdc/account/login'
values = {
"postUserLogin": {
"login": my_email,
"password": my_password,
"remember": 0,
"verify_level": 0
}
}
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
encoded_values = json.dumps(values)
#request = Request(url, data=encoded_values, headers=headers)
r = requests.post(url, data=encoded_values)
return r
That successfully logs me in, returning a 200 response.
Given the documentation from the gooddata website on connecting to the API, I'm trying to export a raw project file.
I set the project and object ids:
project_id = 'asibfakuyebkbhdbfaisdf'
object_id = '87234760'
values = {
"report_req": {
"reportDefinition": "/gdc/md/"+ project_id + "/obj/" + object_id
}
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
url = 'https://secure.gooddata.com/gdc/app/projects/' + project_id + '/execute/raw/'
r = requests.post(url, data=json.dumps(values), headers=headers)
request = Request(url, data=json.dumps(values), headers=headers)
response_body = urlopen(requests).read()
print response_body
I played around with using r = requests.post(url, data=encoded_values and request = Request(url, data=encoded_values, headers=headers). Still receiving an error. I'm not really sure how to tackle next steps.
Following directions as stated in documentation for connecting to the API:
You need to perform all HTTP requests from a single "session" that remembers cookies from the login: perform s = requests.Session() once, then use s.post instead of requests.post.
See https://stackoverflow.com/a/31571805/3407728 for more.
I have this code, it sends a request to my server which is run by web.py:
url = "https://sample.com/api"
auth_string = 'Basic ' + base64.encodestring('%s:%s' % ("usernamexxxx", "codexxxx"))[:-1]
headers = {"Content-Type": "application/json", "authorization": auth_string}
data = {"message":"WELCOME!..."}
req = urlfetch.fetch(url, method=urlfetch.POST, payload= simplejson.dumps(data), headers= headers)
response = req.content
So when I receive this kind of request in my web server I want to check if the username and code input to the request is valid or not? And how can I access the given data of the code above?
Probably you can access those headers via the os.environ dictionary.