Can someone explain how I can pull this API info with python or point me in the right direction?
On the swagger documentation page for the API I am trying to access, I go to the header authentication section; paste in the API key, and am returned with a 'Base XXXXXXXXXXXX" key. Which I learned is an encoded base64 key. Then I paste the Basic key into the API key input filed in another section (See screenshot) and am successful in pulling the API information.
However, I cannot figure out how to pull that information with python requests or postman. When I google "Pulling API with basic API key" It says I need a username and password.
Any help would be appreciated. My apologize for the noob question enter image description here
I found the answer.
from hashlib import new
from unicodedata import name
from urllib import response
import requests
import pandas as pd
url = 'URL'
headers = {'Authorization': 'Basic xxxxxxxxxxx'}
r = requests.get(url,headers=headers )
data = r.json()
Related
I'm trying to fetch metadata from thoughtspot. I am able to call the url using browser and fetch the data. But here I'm trying to achieve it via python program. According to thougthspot documentation. I have to enable trusted authentication and pass my secret key & username to obtain a token which I can use in my program.
https://developers.thoughtspot.com/docs/?pageid=api-auth-session
my username : username#username.com
secret key : secret-key
Below is my code:(generated by postman)
import requests
url = "https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/session/auth/token?auth_token=secret-key&access_level=FULL&username=username#username.com"
payload={}
headers = {}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
I'm getting Bad request error. Anyone here using thoughtspot over this issue. Appreciate your support very much.
Error I'm getting:
{"type":"Bad Request","description":"The server could not understand the request due to invalid syntax."}
I can fetch data by calling the api using a web-browser. Below url returns list of all meta-data objects. I want to achieve this using a python program (I have to authenticate first & call the below URL - Authentication step is not working for me when I tried to follow the documentation)
https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/metadata/list
Did you try changing the url so that it includes the domain name?
Also post the error you are getting. And a screenshot of a working request would be great!
I am writing a python script which calls the API of Luno.com. It will form part of a larger python script(testing the new API call in a separate script) in which I have already made successful calls to a different site's API, however it did not require authentication. Luno documentation says it needs an API key (which I have) and to use the id and key secret as the username and password in a normal HTTP authentication, being new to python I googled how to do basic HTTP authentication and found this:
from requests.auth import HTTPBasicAuth
result = requests.get('url', auth=('username', 'password'))
I tried using this, but can not seem to get it right no matter what I try,I get the following error when printing the response code and the json output:
<Response [404]>
{'error': 'Cannot find that market', 'error_code': 'ErrMarketNotFound', 'error_action': {}}
I will include my python script and also links to the luno API sections which are applicable. The URL I want to access is https://api.luno.com/api/1/ticker , used to get the currency pair.
https://www.luno.com/en/developers/api
https://www.luno.com/en/developers/api#operation/getTicker
Screenshots if you don't want to click the links:
Python Code:
import json
import requests
from requests.auth import HTTPBasicAuth
urlLuno = 'https://api.luno.com/api/1/ticker'
statsAuthLuno = requests.get(urlLuno, auth=('idhere', 'secretkeyhere'))
print(statsAuthLuno)
print(statsAuthLuno.json())
the ticker API does not require authentication but it does require the market pair as seen here
requests.get(urlLuno, params={"pair": "XBTMYR"})
should get you the details you need.
Or you can get all tickers
I tried to fetch data from api endpoint using request, but I got a bad key error. In my case, I provided api-key and endpoint url, so I used request to get json content of the data then used pandas to create data frame. I looked into SO but didn't find a clue how to get over this. Does anyone have possible thoughts or attempts to fix this? How can I correctly fetch data from api endpoint? Any idea?
my attempt
I tried to fetch data from this site: market data
import requests
import pandas as pd
api_key = 'ec95a478-e46e-47f9-b57d-3d19012d527d'
url = 'https://apps.fas.usda.gov/OpenData/api/esr/countries'
headers = {'Ocp-Apim-Subscription-Key': '{key}'.format(key=api_key)}
jsonData = requests.get(url, headers=headers).json()
df = pd.read_json(json.dumps(jsonData)
but I got this error after running the above codes:
'Bad API Key'
I accessed to this api and got this instruction as follow:
Hello Adam, your custom API key is
********---****-3d19012d527d Show Please copy and paste your API key into api_key text box in Swagger window below and start
exploring the API. Please ues your custom key as name value pair
API_KEY:Value in HTTP Header, when making API request from your
applications.
where am I wrong in my above attempt? How can I correctly fetch data from any API endpoint on this site? Can anyone suggest a possible way of fetching data from api endpoints with json, request, and pandas? Any thoughts?
You're sending 1 header with your request. That header is named Ocp-Apim-Subscription-Key. Judging by the sliver of email you shared they expect the header to contain a pair named API_KEY.
If that is the issue the following should resolve it.
headers = {'API_KEY': '{key}'.format(key=api_key)}
Formatting a string to be the value of a string variable is redundant. Your code could be cleaned up by removing the call to format()
headers = {'API_KEY': api_key}
One thing to keep in mind
You posted your API key on a public site. Given that API keys are used for authentication, it would be best practice to de-authenticate your current key and request a new one, to prevent unauthorized use.
I am trying to get GIPHY Dashboard data like :
Total GIF/ stickers Upload
GIF views
When I download/Export the data , the endpoint is:
https://giphy.com/api/v2/users/aggregations/?start_dt=2013-02-01&end_dt=2020-07-08&content_type=gif,sticker
I need to get the same data via API . I also read documentation but not able to find.The documentation only has V1 api whereas above link / endpoint is V2
I am using Python.
Could anyone please help me :)
https://giphy.com/api/v2/users/aggregations/?start_dt=2013-02-01&end_dt=2020-07-08&content_type=gif,sticker&api_key={api_token}
Have you tried to reach this endpoint? Can you enter your api_token here and try it?
import requests
url = '...'
response = requests.get(url)
response.json()
I have access to an API that I'm trying to start leveraging to automate some tasks and I jumped right into it but was stymied by JWT, which I have never used. I'm also coming off a couple years not using python, so I'm a little rusty. Please bear with me.
Here is a direct quote from the API documentation:
The authentication mode for an organization is with a JSON Web Token. Users
must pass a JSON Web Token (JWT) in the header of each API request made.
To obtain the JWT, send the user’s API key (UUID) and password in a JSON Web
Token GET Request. The authorization method of “Bearer” and a
space is then prefixed to the encoded token string returned. The token will
be tied to the user account that generated the JWT.
I've tried with requests but I'm get 405 errors, I've also installed and imported pyjwt but it's confusing to me. This is essentially what I'm trying to send via python:
POST https://<our endpoint>/v1/token/get HTTP/1.1
Content-Type: application/json
{
"username": "<myUsername>",
"password": "<myPassword>"
I've verified that the target API is working, as there is a small set of functionality that works without JWT and was easily accessed via requests
Advice is welcome, as are any tutorials. I've tried to read several JWT tutorials but I'm having a hard time translating it to python.
Thanks!
Question: To obtain the JWT, send the user’s API key (UUID) and password in a JSON Web Token GET Request
Solution using python_jwt.
Assumptions:
Encoding Method = HS256
claims Fieldname 'consumerId'
claims Fieldname 'httpMethod'
Your JWT in the url looks like:
'http://httpbin.org/get?eyJ0eXAiOiAiSldUIiwgImFsZyI6ICJIUzI1NiJ9... (omitted for brevity)
response.json() contains the requested JWT you have to use afterwards.
Note: Your have to use https://<base url>/v1/token/get
import python_jwt as jwt
# Create claims dictionary for generation of JwToken
claims = {
'consumerId': 'My App ID',
'httpMethod': 'GET'
}
import datetime
# create JWToken
jwtoken = jwt.generate_jwt(claims, 'My secret', 'HS256', datetime.timedelta(minutes=5))
response = requests.get('http://httpbin.org/get', jwtoken)
print(response.json())
Tested with Python:3.4.2 - requests:2.11.1