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

Android

Matthew Lee
Matthew Lee
6,715 Points

Basic Authentication on Android?

I am trying to make a call to an API which requires a basic authentication. How can I accomplish this using OkHttp3? OR Could anyone provide some sample code to work off of?

Best, Matt

Justin Roberts
Justin Roberts
14,583 Points

Probably need a little more information. What type of request is it (ex GET, POST...)? What type of authentication? Usually you'll have to read the API documentation to figure out how they authenticate users. What's the API you want to use? Usually authentication requires adding either a header or query parameter. Here's a good tutorial I found for using OkHttp3: https://guides.codepath.com/android/Using-OkHttp . But if you let me know the details I can see if I can help you out.

Matthew Lee
Matthew Lee
6,715 Points

Hey Justin,

Thanks so much for the quick reply. The link you provided is exactly what I was looking for! It was a GET request from an API my friend built but I was confused with the header part.

It works well now. Thanks!!

Justin Roberts
Justin Roberts
14,583 Points

No problem, glad to hear you figured it out.