I am trying to send some data via a web based api. The client has given me an example snippet of code to test everything.
My problem below is how to use my certificate.
Their example
response = requests.post(url, data=data, headers=headers, cert=('certificate.cer','keyfile.key'))
I tried
response = requests.post(url, data=data, headers=headers, cert=('C:\MyPath\My-certs.p12','password'))
However I get the error,
OSError: Could not find the TLS key file, invalid path: password
I have written a C# example and the code works, so I know the certificate and the other pieces are ok. I just can't get the certificate to work in python
Your certificate file is P12, which isn't supported by requests yet. You have two options:
Convert the P12 file into separate public certificate and private key files. For Linux or Mac, use this. Then you can use the separate files as a tuple in cert.
Use requests-pkcs12 which has support for P12 files:
--
from requests_pkcs12 import post
response = post(url, data=data, headers=headers, pkcs12_filename='C:\MyPath\My-certs.p12', pkcs12_password='password')
Related
I am using python's request to make a post call like below -
response = requests.post(ENDPOINT, data=json.dumps(data), \
headers=headers, proxies=getProxy(), verify=True)
What does this 'verify=True' actually verify internally with respect to the certificate? what all information it verifies - whether it includes common name as part of verification?
Is there any other ways in python this verification can be done?
thanks for your replies
Good day. I'm writing a python program that requests some posts from my Facebook page. To do so, Facebook offers a tool that they call "Graph API Explorer". Using something similar to a GET request, I can get anything that I want (granted that I have access and a valid token). I've come up with my own solution for the Graph API Explorer and that is generating my URLs. After generating a URL, I use pycurl to get a JSON object from Facebook that contains all of my data.
When I use pycurl, I get the following error:
pycurl.error: (3, 'Illegal characters found in URL')
but when printing said URL and pasting it to a browser, I got a valid response.
URL: https://graph.facebook.com/v7.0/me?fields=posts%7Bmessage%2Cfrom%7D&access_token=<and my access token which is valid>
my code looks like this:
def get_posts_curl(nodes=['posts'], fields=[['message', 'from']], token_file='Facebook/token.txt'):
curl = pycurl.Curl()
response = BytesIO()
token = get_token_from_file(token_file)
# constructing request.
url = parse_facebook_url_request(nodes, fields, token)
url = convert_to_curl(url)
print("---URL---: " + url)
# curl session and settings.
curl.setopt(curl.CAINFO, certifi.where())
curl.setopt(curl.URL, url)
curl.setopt(curl.WRITEDATA, response)
curl.perform()
curl.close()
return response.getvalue().decode('utf-8')
The error pops up at curl.perform()
Some info that might be relevant:
All was working great a while ago. After transferring my program from my workstation (that is running Windows 10) to my server (Ubuntu 18.04 Server) still, all was working fine and I placed that project to the side. Only now that error pops up and I haven't touched the project in a while.
It seems that the token is causing the issue. I've tried about 100 tokens and some cause the problem and some don't. Also, a fix that solved it all was using urllib3.unquote
from urllib.parse import unquote
...
url = unquote(url)
I am trying to write automation scripts for get request.
As part of the request i need to send secure key in headers. I am giving following details in test
url = "some url"
headers = {"key":"12345"}
response = requests.post(url, json_request, headers=headers)
At the moment i'm exposing header details. I am trying to get them saved to environ.env file and import them into config file. But i have no luck.
environ.env file
test_headers = {"key":"12345"}
config file
import os
headers = {"headers": os.environ.get("test_headers")}
Unable to get the code working. Headers are not recognised while executing code.
Any help would be greatly appreciated.
Thanks,
Got this to be working
environ.env file
KEY:"12345"
config file
import os
headers = {"headers": os.environ.get("KEY")}
I have recently been working with API's but I am stuck on one thing and it's been holding me back for a few days.
I am trying to work with Privacy's API and I do not understand the Authentication/Authorization process. When I enter the url in a browser I get the error "message": "Please provide API key in Authorization header", even when I use the correct format of Authorization. I also get an error when I make a request in Python. The format I'm using for the url is https://api.privacy.com/v1/card "Authorization: api-key:".
If someone could explain how to work this or simply give an example of how I would make a request through Python3. The API information is in the link below.
Thank you in advance.
https://developer.privacy.com/docs
This is the code I am using in Python. After I run this I receive a 401 status code.
import requests
headers={'Authorization': 'api-key:200e6036-6894-xxxx-xxxx-xxxx'}
url = 'https://api.privacy.com/v1/card'
r = requests.get(url)
print("Status code:", r.status_code)
You need to add the authentication header to the get call. It isn't enough to include it in a header variable. You need to provide those headers to requests
import requests
response = requests.get('https://api.privacy.com/v1/card', headers={'Authorization': 'api-key 65a9566c-XXXXXXXXXXXX'})
print(response.json())
I'm using SpeechRecognition library.
import speech_recognition as sr
AUDIO_FILE = 'test_audio.wav'
with open("api-key.json") as f:
GOOGLE_CLOUD_SPEECH_CREDENTIALS = f.read()
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source)
print('Starting recognition...')
print(r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS))
print('Completed')
When above code is run, an error occurs -
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify
failed (_ssl.c:777)
The audio file and api-key files are in place.
I managed to consume through proxy by directly editing the code of google speech client library in python. Specifically I edited the file at(it might be different in your case):
lib/python3.6/site-packages/google/auth/transport/requests.py
the class Request, method call, there is a line like:
response = self.session.request(method, url, data=body, headers=headers, timeout=timeout)
I added the parameter verify=False to that call which will just ignore SSL certificate verifications. However that is not recommended since incurs in security issues. If you happen to have the certificates of the CA in the proxy you replace verify=False with cert="/local/address/to/ca/cert". Here is how I have it working:
response = self.session.request(method, url, data=body, headers=headers, timeout=timeout,verify=False,**kwargs)