I am trying to do a python request get command using python. I have:
endpoint = https://eventsapi.uatxyzreports.com/api/v1/Events/OR/12345
headers = {"Authorization":"abcxyz12345"}
response = requests.get(endpoint, headers = headers)
the response code is ("401")
but when I put in the same token I got a response code of 200. Where did it go wrong?
Related
I'm trying to submit a get request using the following code in AWS Lambda.
hed = {'Authorization': f"bearer {token}"}
URL = "https://integrate.elluciancloud.com/api/academic-periods"
r = http.request('GET', URL, headers=hed)
print(r.data)
The Token which I'm passing is : iLCJ0b2ciOlsiRVhDSEFOR0VfREV.TSUdORVJfRU5BQkxFRCIsIlBBQ0tBR0VTX0ZFQVRVUkVTX0VOQUJMRUQiXSwicm9sZXMiOlsiYWZlZWYyMDQtZTM3MS00MTlhLWE4OTAtOTNlNjYyMWE0MjM4Il0sInRlbmFudCI6eyJpZCI6IjcyMjJhZmEwLWYzNGYtNDRhNy1hZWVmLTEzZDE5NWJkNTQ4YSIsImFjY291bnRJZCI6IkludGVybmFsRWxsdWNpYW5CYW5uZXJNb2Rlcm5pemF
But I'm the following error message in r.data
b'{"errors":[{"code":"General.error","description":"Application error","message":"java.lang.IllegalArgumentException"}]}'
I tried to encode the token (token = token.encode('ascii', 'ignore')) but it's not working
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 run a search on a website for the word 'Adrian'. I already understand that first I have to send a request to the website, in the response I will have an XSRF-token that I need to use for the second request. As I understand, if I'm using session.get(), it keeps the cookies automatically for the second request, too.
I run the first request, get a 200 OK response, I print out the cookies, the token is there. I run the second request, I get back a 400 error but if I print out the header of the second request, the token is there. I don't know where it is going wrong.
Why do I get 400 for the second one?
import requests
session = requests.Session()
response = session.get('https://www.racebets.com/en/horse-racing/formguide')
print(response)
cookies = session.cookies.get_dict()
print(cookies)
XSRFtoken = cookies['XSRF-TOKEN']
print(XSRFtoken)
response = session.get('https://www.racebets.com/ajax/formguide/search?s=Adrian')
print(response)
print(response.request.headers)
I also tried to skip session and use requests.get() in the second request and add the token to the header by myself but the result is the same:
import requests
session = requests.Session()
response = session.get('https://www.racebets.com/en/horse-racing/formguide')
print(response)
cookies = session.cookies.get_dict()
print(cookies)
XSRFtoken = cookies['XSRF-TOKEN']
print(XSRFtoken)
headers = {'XSRF-TOKEN': XSRFtoken}
response = session.get('https://www.racebets.com/ajax/formguide/search?s=Adrian', headers=headers)
print(response)
print(response.request.headers)
As Paul said:
The API you're trying to make an HTTP GET request to cares about two request headers: cookie and x-xsrf-token. Log your browser's network traffic to see what they're composed of.
The header needs to be named x-xsrf-token. Try this:
token = session.cookies.get('XSRF-TOKEN')
headers = {'X-XSRF-TOKEN': token}
response = session.get('https://www.racebets.com/ajax/formguide/search?s=Adrian', headers=headers)
I'm having an issue converting a working cURL call to an internal API to a python requests call.
Here's the working cURL call:
curl -k -H 'Authorization:Token token=12345' 'https://server.domain.com/api?query=query'
I then attempted to convert that call into a working python requests script here:
#!/usr/bin/env python
import requests
url = 'https://server.domain.com/api?query=query'
headers = {'Authorization': 'Token token=12345'}
r = requests.get(url, headers=headers, verify=False)
print r
I get a HTTP 401 or 500 error depending on how I change the headers variable around. What I do not understand is how my python request is any different then the cURL request. They are both being run from the same server, as the same user.
Any help would be appreciated
Hard to say without knowing your api, but you may have a redirect that curl is honoring that requests is not (or at least isn't send the headers on redirect).
Try using a session object to ensure all requests (and redirects) have your header.
#!/usr/bin/env python
import requests
url = 'https://server.domain.com/api?query=query'
headers = {'Authorization': 'Token token=12345'}
#start a session
s = requests.Session()
#add headers to session
s.headers.update(headers)
#use session to perform a GET request.
r = s.get(url)
print r
I figured it out, it turns out I had to specify the "accept" header value, the working script looks like this:
#!/usr/bin/env python
import requests
url = 'https://server.domain.com/api?query=query'
headers = {'Accept': 'application/app.app.v2+json', 'Authorization': 'Token token=12345'}
r = requests.get(url, headers=headers, verify=False)
print r.json()
All, I'm trying to implement a curl request to get data from the BLS. Following their example here (they show the curl request), my code looks like this:
import requests
headers = {'Content-type': 'application/json'}
params = {"seriesid":["LEU0254555900", "APU0000701111"],"startyear":"2002", "endyear":"2012"}
p = requests.post('http://api.bls.gov/publicAPI/v1/timeseries/data/', params = params,headers = headers)
print p.url
print p.content
I'm getting the following (error) output:
http://api.bls.gov/publicAPI/v1/timeseries/data/?seriesid=LEU0254555900&seriesid=APU0000701111&endyear=2012&startyear=2002
{"status":"REQUEST_FAILED","responseTime":0,"message":["Sorry, an
internal error occurred. Please check your input parameters and try
your request again."],"Results":null}
Anyone had to deal with the BLS api and python?
Is the requests library the best for this?
You need to send the data as json, not pass it as a params dict. params sets the url parameters, which is not what you want, you need to pass it as data.
This should work:
import requests
import json
headers = {'Content-type': 'application/json'}
data = json.dumps({"seriesid":["LEU0254555900", "APU0000701111"],"startyear":"2002", "endyear":"2012"})
p = requests.post('http://api.bls.gov/publicAPI/v1/timeseries/data/', data=data, headers=headers)
print p.url
print p.content