Trying to get the access token in response for google but can't figure out what I'm doing wrong.
This is what I've done so far:
import requests
authorization_code_req = {
'client_id':'key',
'client_secret':'key',
'redirect_uri':'http://localhost:5000',
'grant_type':'authorization_code'}
r = requests.get('https://accounts.google.com/o/oauth2/token',
params=authorization_code_req)
print (r)
you should send authorization_code_req as json and not as params. So your code should look something like this:
import requests
authorization_code_req = {
'client_id':'key',
'client_secret':'key',
'redirect_uri':'http://localhost:5000',
'grant_type':'authorization_code'
}
r = requests.get('https://accounts.google.com/o/oauth2/token',
json=authorization_code_req)
print(r)
Related
im new in this of "Calling APIs", so i don't understand a lot, I was wondering if anyone can help me.
I want to sort the cryptocurrencies from coinmarketcap whit their API, in the Api documentation, says that there is a parameter 'sort', and i can use differents values, like 'name','symbol' and 'recently added'. So if i want to get the last cryptocurrency listed, i have to sort whit 'recently added'. But i dont know how to do it. Here is a piece of code that i can write, but whit that code i get All the cryptocurrencys and i dont know how to sort it. Thanks
import requests
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest"
parameters = {
'start':'1',
'limit':'5000',
'convert':'USD'
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': 'private key',
}
session = Session()
session.headers.update(headers)
response = session.get(url, params=parameters)
print(response.json())
I guess its
parameters = {
'start':'1',
'limit':'5000',
'convert':'USD',
'sort':'recently_added'
}
assuming you interpretted the docs right
it actually looks like it want 'sort':'date_added' NOT 'recently_added' based on the docs at https://coinmarketcap.com/api/documentation/v1/#operation/getV1CryptocurrencyListingsLatest
I'm not sure what i'm doing wrong with the script below, but i keep getting this error when i try to execute the script. any idea what i'm doing wrong? thanks!
import requests
blxr_endpoint = "https://bxgw-nd-061-866-537.p2pify.com"
authorization = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
blxr_request_json = {
"method": "blxr_tx",
"params": {
"transaction": signed_txn.rawTransaction.hex()[2:],
}
}
result = requests.post(blxr_endpoint,json = blxr_request_json,auth = authorization)
Less familiar with the auth param , but from requests docs the auth param doesn't get string as an input.
It supposed to get a class that inherits from requests.auth.AuthBase like:
HTTPBasicAuth
HTTPDigestAuth
OAuth1
and other(you can also create one yourself.)
Another option is to insert the Authorization header of the http manually:
result = requests.post(blxr_endpoint, headers={'Authorization': authorization})
I'm trying to play with Petfinder's API, which recommends using curl to get a token key instead of requests. How can I convert their recommended curl code into Requests specific language that works? THe documentations is listed here https://www.petfinder.com/developers/v2/docs/#using-the-api
Recommended curl code:
curl -d "grant_type=client_credentials&client_id={CLIENT-ID}&client_secret={CLIENT-SECRET}" https://api.petfinder.com/v2/oauth2/token
I tried the below (iterating through combos of CLIENT-ID and CLIENT-SECRET, but to no success.
import requests
params = {params = {'CLIENT-ID': XXXXXXXXX,
'CLIENT-SECRET': XXXXXXXX}
}
r = requests.get('https://api.petfinder.com/v2/oauth2/token', params = params)
And I received an error 401 message that indicates that my authorization isn't sufficient.
try like this:
import requests
client_id = ""
client_pass = ""
data = {"grant_type":"client_credentials"}
api_url = "https://api.petfinder.com/v2/oauth2/token"
r = requests.post(api_url,data=data,auth=(client_id,client_pass))
r = r.json()
I have a basic API installed as my localhost server that does functions such as add camera, star camera, list cameras, snapshot of camera frame, etc.
My problem is after following the documentation I still can't seem to interact with it well and get the response I need. Here is the code I use to log in and get validation token:
import requests
import urllib.request
import json
base_url = "http://localhostip:8080/api/user/login?"
parameters = {
"username": username,
"password": password
}
auth_tok = requests.post(base_url + urllib.parse.urlencode(parameters)).json()
print(auth_tok)
I get the correct documented response with a token, so following the documentation to add camera I need 2 parameters, URL and Name, so I did:
base_url = "http://localhostip:8080/api/camera/add?"
parameters = {
"url": 'rtsp://192.168.1.23/1',
#or video file
"url" : '/home/video/sample.mov'
"name" : 'cam1'
}
r = requests.post(base_url + urllib.parse.urlencode(parameters),headers={'Authorization': auth_tok})
when I print the response:
-print (r)
-print (r.url)
-print(r.status_code)
-print(r.json())
I get this:
<Response [500]>
http://192.168.0.162:8080/service/api/camera/add?url=rtsp%3A%2F%2Frtsp%3A%2F%2F192.168.1.23&name=cam1
500
{'code': -111, 'message': None}
According to documentation the correct url should be like this:
http://192.168.0.6:8080/service/api/camera/add?url=rtsp://192.168.1.23&name=cam1
and the response should be:
Response:
{"status":"ok"}
So why and how to make the URL POST in the correct format, because I suspect this is the issue, the URL has these encoding symbols that may be messing up the request?
When I use the web browser GUI of this API I can add the camera or even a video file to play but I'm trying to do the same with Python so I can do further processing in future.
Your problem is when you encode the ' / / ' symbol, so, in order to fix that, you need to use another function from urllib, urllib.parse.unquote(), and use as parameter your encoding function urllib.parse.urlencode(parameters):
import urllib
parameters = {
"url": 'rtsp://192.168.1.23/1',
"name" : 'cam1'
}
The results are :
print(urllib.parse.urlencode(parameters))
'url=rtsp%3A%2F%2F192.168.1.23%2F1&name=cam1'
print(urllib.parse.unquote(urllib.parse.urlencode(parameters)))
'url=rtsp://192.168.1.23/1&name=cam1'
Source https://docs.python.org/3.0/library/urllib.parse.html#urllib.parse.unquote
I'm using pythons requests library to make a get request that returns a arraybuffer.
when I use requests.get(url), I end up getting back the text response which is very different from the arraybuffer response I am looking for.
I am able to get the arraybuffer response in node / js with this
req.onload = function(event) {
var res = req.response;
console.log(res)
}
req.open("GET", url, true);
req.responseType = "arraybuffer";
res = req.send()
However, if req.responseType = "arraybuffer" is excluded I get the incorrect text response, similar to in python
what is this equivalent to req.responseType = "arraybuffer" in python requests (or another python http library)?
Thanks a bunch
import requests
r = requests.get(url)
retArray = [int(i) for i in r.content]
# It seems that variable retArray is much like the arraybuffer in the browser.
# you can try it out.