I'm having trouble working with an api. I was given a password and the header (x-app-id) I was given little documentation on the api but below it is shown how to retrieve the data using curl (from documentation):
curl -X GET "https://r6db.com/api/v2/players?
name=Bomb&platform=ps4&exact=true" -H "accept: application/json" -H "x-app-
id: password"
Above returns information on a player named 'Bomb' on the platform 'ps4' relating to the game Rainbow six siege. exact = true means tat the name has to be exact.
Can't give password for security reasons. Here is my try at replicating above in python going off another post I saw
import json
import requests
URL = "https://r6db.com/api/v2/players?name=Bomb&platform=ps4&exact=true"
KEY = "password"
headers = {"x-app-id": "application/json"}
params = {
"a-app-id": KEY
}
resp = requests.post(URL + "login/", headers = headers,
data=json.dumps(params))
if resp.status_code != 200:
print('error: ' + str(resp.status_code))
else:
print('Success')
Needless to say it doesn't work (it outputs error 404) which probably means I haven't even managed to get x-app-id as the header, any help appreciated thanks.
I think you have made a bit of a mess when passing the parameters with the module requests
There are two things to pass in the headers: accept: application/json and x-app-id: password and you are doing that fine in curl.
But in python you are passing the header "x-app-id": "application/json" (makes not much sense) and try to pass "a-app-id": KEY} as a parameter, not as a header.
If you pass it as a parameter, it will be included in your url in the form of https://r6db.com/api/v2/players?name=Bomb&platform=ps4&exact=true&a-app-id=KEY and that is not what you want.
You already know how to pass headers in python, so you only have to pass the right ones: Pass accept: application/json and x-app-id: password
If I got your description right, I think you donĀ“t have to pass data parameters.
Related
I'm trying to use requests to login to a site, navigate to a page, and scrape some data. This question is about the first step (to get in).
I cannot fetch the token from the site:
import requests
URL = 'https://coderbyte.com/sl'
with requests.Session() as s:
response = s.get(URL)
print([response.cookies])
Result is empty:
[<RequestsCookieJar[]>]
This make sense according to the response I'm seeing in Chrome's dev tools. After I login with my username and password, I see four tokens, three of them deleted, but one valid:
How can I fetch the valid token?
you can use the post method to the url you want in order to fetch the token (to pass the login first). For example :
url = "url-goes-here"
url_login = "login-url-goes-here"
with requests.Session() as s:
# get the link first
s.get(url)
payload = json.dumps({
"email" : "your-email",
"password" : "your-password"
})
headers = {
'Content-Type': 'application/json'
}
response = s.post(url=url_login, data=payload, headers=headers)
print(response.text)
Based on your question, i assume that if you only use username or password to login, then you can use HTTPBasicAuth() which is provided by requests package.
I am trying to convert a Curl POST request into a python script, and i am not getting the desired output, please let me know what i am doing wrong here.
CURL request
curl -s -w '%{time_starttransfer}\n' --request POST \
--url http://localhost:81/kris/execute \
--header 'content-type: application/json' \
--data '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
This runs the uptime command in the node for which the ip is provided and returns a JSON output:
{"command":"uptime","output":["{\"body\":\" 17:30:06 up 60 days, 11:23, 1 user, load average: 0.00, 0.01, 0.05\\n\",\"host\":\"10.0.0.1\"}"]}0.668894
When i try to run the same with python, it fails and never gets the output
Code :
import urllib3
import json
http = urllib3.PoolManager()
payload = '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
encoded_data = json.dumps(payload)
resp = http.request(
'POST',
'http://localhost:81/kris/execute ',
body=encoded_data,
headers={'Content-Type': 'application/json'})
print(resp)
I would recommend you use the requests library. It's higher level than urllib and simpler to use. (For a list of reasons why it's awesome, see this answer.)
Plus it requires only minor changes to your code to work:
import requests
payload = '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
resp = requests.post(
'http://localhost:81/kris/execute',
data=payload,
headers={'Content-Type': 'application/json'})
print(resp.text)
Note that the method POST is the function instead of a parameter and that is uses the named param data instead of body. It also returns a Response object, so you have to access its text property to get the actual response content.
Also, you don't need to json.dumps your string. That function is used to convert Python objects to JSON strings. The string you're using is already valid JSON, so you should just send that directly.
Here is an online utility you can check out to convert curl requests to python code.
Curl to python converter
Another alternative is Postman application. There you will have the option to convert curls to code for various languages, in the code section.
It a very good practice to check if the api requests are working by running the curl in postman.
And for your case, here is the code using python requests library.
import requests
headers = {
'content-type': 'application/json',
}
data = '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
response = requests.post('http://localhost:81/kris/execute', headers=headers, data=data)
Hope that helps! Happy Coding!
I am working with the CF API RESTful services. Trying to get an access token from cloud foundry's UAA API using https://login..../oauth/token web method.
I have verified that headers & body content is correct, but calling the api always returns a 400 error code with message missing grant type.
I have implemented this call in Objective-C, Swift & now Python. All tests return the same result. Here is my code example in Python:
import json
import requests
import urllib
params = {"grant_type": "password",
"passcode": "xxx"
}
url = "https://login.system.aws-usw02-pr.ice.predix.io/oauth/token"
headers = {"Authorization": "Basic Y2Y6", "Content-Type": "application/json", "Accept": "application/x-www-form-urlencoded"}
encodeParams = urllib.parse.urlencode(params)
response = requests.post(url, headers=headers, data=encodeParams)
rjson = response.json()
print(rjson)
Each time I run this, I get the response
error invalid request, Missing grant type
Any help would be greatly appreciated.
Your code mostly worked for me, although I used a different UAA server.
I had to make only one change. You had the Accept and Content-Type headers flipped around. Accept should be application/json because that's the format you want back, and Content-Type should be application/x-www-form-urlencoded because that's the format you are sending.
See the API Docs for reference.
import json
import requests
import urllib
import getpass
UAA_SERVER = "https://login.run.pivotal.io"
print("go to {}/passcode".format(UAA_SERVER))
params = {
"grant_type": "password",
"passcode": getpass.getpass(),
}
url = "https://login.run.pivotal.io/oauth/token"
headers = {
"Authorization": "Basic Y2Y6",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
}
encodeParams = urllib.parse.urlencode(params)
response = requests.post(url, headers=headers, data=encodeParams)
rjson = response.json()
print(json.dumps(rjson, indent=4, sort_keys=True))
I made a couple other minor changes, but they should affect the functionality.
Use getpass.getpass() to load the passcode.
Set the target server as a variable.
Pretty print the JSON response.
The only other thing to note, is that the OAuth2 client you use must be allowed to use the password grant type. It looks like you're using the same client that the cf cli uses, so if your UAA server is part of a standard Cloud Foundry install that is likely to be true, but if it still doesn't work for you then you may need to talk with an administrator and make sure the client is set up to allow this.
I am trying to pull data from github api using specific search query and put it into a .txt file. I am able to do so by through curl and shell but I need to do it in python which I am very unfamiliar with. I have seen the requests library
I've tried using this website https://curl.trillworks.com/ and using requests library but I can't seem to wrap my head around how to format the request.
curl -H "Authorization: token xxx" 'https://api.github.com' "https://github.com/api/v3/search/repositories?q=Evidence+locker+Seed+in:readme" > evidencelockerevidence.txt
The above code does exactly what I need it to do (passes GHE token, calls api, stores it in a file) I just need help converting to python please.
EDIT: Solution was
import requests
headers = {
'Authorization': 'token xxx',
}
url = 'https://github.ibm.com/api/v3/search/repositories?q=Evidence+locker+Seed+in:readme'
response = requests.get(url, headers=headers)
print(response)
print(response.text)
All -H tags need to be specified in the headers key, and the url is the first positional argument.
import requests
headers = {
'Authorization': 'Bearer bearer_token',
}
url = 'https://github.com/api/v3/search/repositories?q=Evidence+locker+Seed+in:readme'
response = request.get(url, headers=headers)
print(response)
print(response.text)
That handles getting the data, now you have to worry about writing it to a file, which you can do with the open built-in function:
with open('path/to/evidencelockerevidence.txt', 'w') as file:
file.write(response.text)
The Ecobee API documentation shows this as a way to access their API:
#curl -s -H 'Content-Type: text/json' -H 'Authorization: Bearer AUTH_TOKEN' 'https://api.ecobee.com/1/thermostat?format=json&body=\{"selection":\{"selectionType":"registered","selectionMatch":"","includeRuntime":true\}\}'
I have used that code in curl and it seems to work.
However when I try what I think is the equivalent python code it doesn't work.
(I really don't know curl well at all. What I know I know from a few hours of internet research.)
the code I am using:
import requests
headers = {"Content-Type": "text/json", "Authorization": "Bearer AUTH_TOKEN"}
response = requests.get('https://api.ecobee.com/1/thermostat?format=json&body=\{"selection":\{"selectionType":"registered","selectionMatch":"","includeRuntime":"true"\}\}', headers=headers)
print(response.text)
When I send this I get:
{
"status": {
"code": 4,
"message": "Serialization error. Malformed json. Check your request and parameters are valid."
}
}
Not sure what could be wrong with my json formating. Any help is much appreciated.
You'll need to URL-escape the special characters in the parameters.
Doing this by hand can be messy and prone to mistakes. I'm not a Python expert but initial research suggests using the params option built into Python's request.get(). For example:
import requests
url = 'https://api.ecobee.com/1/thermostat'
TOKEN = 'ECOBEEAPIACCESSTOKEN'
header = {'Content-Type':'text/json', 'Authorization':'Bearer ' + TOKEN}
payload = {'json': '{"selection":{"selectionType":"registered","selectionMatch":"","includeRuntime":"true"}}'}
response = requests.get(url, params=payload, headers=header)
print(response.url)
print(response.text)