Make a request the Basic authorization from a PyPortal

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
rhammell
 
Posts: 6
Joined: Wed Jun 27, 2012 6:41 am

Make a request the Basic authorization from a PyPortal

Post by rhammell »

I'm attempting to request data from an API using a Python script running on my PyPortal. The API requires that the user's credentials be passed using Basic authentication.

I've seen this way of doing it using Python:

Code: Select all

headers = {'Authorization': 'Basic ' + base64.b64encode('username:password')}
r = requests.get('http://myapi.com', headers=header)
The problem here is that the 'base64' library isn't available on the PyPortal.

I've also tried passing the auth in the URL:

Code: Select all

r = requests.get('http://username:[email protected]')
Although something like this works in Postman, it causes an error when trying it on the PyPortal.

Is there a way to implement Basic authentication?

User avatar
rhammell
 
Posts: 6
Joined: Wed Jun 27, 2012 6:41 am

Re: Make a request the Basic authorization from a PyPortal

Post by rhammell »

Was able to find a solution:

With a little more digging I found the circuitpython-base64 library (https://github.com/jimbobbennett/CircuitPython_Base64), which is included in the CircuitPython Community Library Bundle.

Copying this library over to the PyPortal I'm able to get it working with the following:

Code: Select all

from circuitpython_base64 import b64encode

auth_credentials = username + ":" + password
auth_token = b64encode(auth_credentials.encode("utf-8")).decode("ascii")
headers = {'Authorization': 'Basic ' + auth_token}

r = requests.get(http://myapi.com, headers=headers)

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”