Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

How would I go about using flask HTTP basic auth to send login details to external api?

I have an app, that I want to restrict all of the pages so that only logged in people can see these pages.

However as I am connecting to an api that already has the users and passwords stored in there databases. All I need to do is send the post data from the login to the api. But I am getting confused on how to do this in flask.

Can anyone shed some light?

Kenneth Love maybe you could help out?

1 Answer

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Sounds to me like Flask isn't really a part of this other than getting the details submitted to it? So I would think you'd take whatever data comes in through the form/auth/whatever and send it to the API, right? Then, depending on the API's response, the user is logged in or not? I'm not sure how you'd do the "this person has a valid account" part, though. Is it a public service that you're using? If so, maybe check to see if there's a package already for that API and Flask's auth?

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Kenneth Love I managed to resolve it, I thought that there was going to be two apis that I was connecting to. One that I pass the email and password, and get a secure login. And a second where I send post data to. However the guy who built the api said no there is only one api to handle the post data, but also has authorization token I used;

auth = requests.authorization 

You can see my code here if you can be bothered. I used the flask snippet for the basic http auth at the beginning of the file.

Then at the end there is a function convertToJson which handles the post request to the api.

Now I just need to figure out how to deploy it on an ubuntu server! This was a project I got randomly asked to do at work as they knew I was learning to code. Has been exciting.