1

I am connecting through an API to receive data. From the website API documentation, the instructions use either two CURL methods to connect the API; however, I need to connect using python.

1st Method
Curl Example

curl -d '' -X POST 'https://api.bcda.cms.gov/auth/token' \
    -H "accept: application/json" \
    -H "authorization: Basic <Client_Secret>"

My Python Conversion:

import requests
import json

url = 'https://api.bcda.cms.gov/auth/token'
       headers = {"accept": "application/json", "authorization": 'Basic',
            '<API_Key>': '<API_Secret>'}

r = requests.post(url = url, data ={}, headers = headers)
   print(r)

2nd Method Curl

curl -d '' -X POST 'https://api.bcda.cms.gov/auth/token' \
    --user <Client_Key>:<Client_Secret> \
    -H "accept: application/json"


My 2nd Python conversion code:

import requests
import json

url = 'https://api.bcda.cms.gov/auth/token'

user = {"<Client_Key>":"<Client_Secret>", "accept": "application/json"}

r = requests.post(url = url, headers = user)

print(r)

I am receiving a 403 connection error, meaning "response status code indicates that the server understands the request but refuses to authorize it."

1

1 Answer 1

1

You should use auth parameter and not headers to convert --user option

headers = {'accept': 'application/json'}
r = requests.post(url=url, headers=headers, auth=(client_key, client_secret))
Sign up to request clarification or add additional context in comments.

14 Comments

That worked! After you receive the "Client Token", How do you add this into the scripting?
I suppose you have to use headers={'Authorization': client_token} at each request. The client token is probably returned in r
Does the client_token go in the header or in the auth?
In the header for sure.
So kinda like this? headers = {'accept':'application/fhir+json', 'Prefer': 'respond-async', 'Authorization': 'Bearer {client_token}}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.