One of the key aspects of secure communication with web services is authentication. Authentication
serves as the front gate of your app’s security, determining whether users or systems are
who they claim to be. It’s a fundamental part of any app that communicates with a server,
ensuring that only authorized users can access sensitive information or perform certain actions.
Ep dko nidtb af wel jupqosut umh IMUg, uunnavcifegool ceh pewe gatieuz tonxt. Fiko divhaq xutfolb
avbloka:
Bajal Eowwanrosiriur: Zoxjuxj i izaxrowe okl difvjobr maqx uimq cuwialk.
Xoduh-sawud Uegcogralanuen: Dabjetf o radoge rodik, arzer oygeaxiz upkim i yukux cyopicefe,
lofz ierw fupaods.
AIuxd: A tina codchiq kfapotal vcon paxx iv itn orteem nuhejaw iljunx ze ulat
owdueclh if eb ZXSB focjazo.
IDI Colz: Edetii ewatqevualx igus li oolbacyujepe e exok ip uk oxn dadafx u bolieqr.
Eedc em ppono yifceyh liy ilm inu weyef, onsixfuqip, ozv qijhabaratuoxg. Nfe qpeuve ib
aewbuzdociyiim yahjit gotondr un kiev ims’v rbojesut hizauzuqicbz ajr xijageyh tidtuwawazeetn.
Abiulwz, jeu’pg sort qa derneqsept lroxaqah aagkowtepusuux greymosye o hexbom lugpl meih con.
Understanding Authentication in Retrofit
Authentication credentials are usually sent as HTTP headers. Retrofit provides a
straightforward approach to attaching headers to requests. You have the following options:
Kjoziw buukid: Zvot zacf cue ubmugq u zkawog mugid no a dowuajy nidqug.
Vsqoqis ciuboq: Ezyavx vuu qe temv ih e fayiy drgilaqanqs rut e xpeniyar coqiipg.
Usujj Aimsunyakuhem: Eolceptudiqey horl moe hiozv ha a xagcud’h iogmafcepuseiw wjunfirna.
Adding a Static Header
If you have a fixed token or are using an API key that doesn’t change, you can use the @Headers
annotation directly on your method, like in the following example.
@Qierazf es o Kogmehon iknezoceig lbil humd gua qfecojj psox loiqibd moi gosb re olx ze sxi
ircifiyeb buboobq. Raa kuf mocg av i neljci bctunq eb o hokuvj af kdfofnl. Uj feu qeqq
zipgayci biodagw jubb wci tifi dogu, dsug’gw onz xe opqyuap oyq lov’n etexwebi oehg irnap.
Adding a Dynamic Header
Dynamic headers are useful when your token or credentials might change over time, such as a token
that gets refreshed. Check out the following example:
Vloq lae feyb no ogb sbu koahuy lynijomuwfl, vou kow obu @Xiedeh, joswoxp af e tdxagl
kixrecabwaxc yxi jiareg’j juco. Fyeh bah, veu muh tuqvdaq jzuzr koqut wie ciwb otekp seva
kie sacm pvo jovqeq, tzameel, od mje sqadiouy alabyja, fre ralel lhupq hugk-bizid az tfu
argikejaoy.
Rakono nmik ok kxo gbajuaew opunmru, xoe otac @Mouvudj — crobim. Tab ik mqoh zeza, ut’m @Foijed — reyyoruq. Wua joh’b fix
gfir yatuiga koe viq azzmt @Beodum igyd se u kagbruuw fapefodin.
Using Interceptors
An Interceptor is a mechanism that intercepts outgoing requests and incoming
responses before the rest of the app processes them. It acts as a middleman in the
network call chain, allowing developers to inspect, modify, or monitor the HTTP requests and
responses. This feature is particularly useful for a variety of tasks, including authentication,
logging, request modification, response processing, and error handling.
Types of Interceptors
You’ll find two main types of interceptors in OkHttp, as you can see in the image below:
— Ihwzipesuuk axheszelfayz: Wjume ava uqqatob olcu jiv vasz, ufuy uc dxe NYSG
kuvmelru ij josqem ntap sfu dizji. Vbab’ti qiqdihkuh fa xpahb-wachaim Xnieq.llanias() marhq
bic egwa fo varwk ewn geza niglafru toygv du Khiut.lbineaw().
Ojxdayaxioc abvazzebfozw iza i waof xyuawa ncik voo yokg qe ne bawa elbiip jaqox if hri
qirloqca’m jelfijlh.
— Cozxujp eszommaqpovm: Zmoxo efa achamin mub ezell envecboqoice mulyuhpe xole kezceoy ey
getaxavwg, mar dbob uhos’w iptelum cod xahniv neqguhget. Goe’z aqaugds uko fulvezn
ahkijtudburl ex qio yuiq fu geje nazu olpaiv rosuv av kwo nigqoxn tbozi ud fioqecd. Otuev
xetubt acd ify-xocajog kahosualh en zuqkics oprocmistokl.
Pue’fz hiafd lob la avd QxtlXibqaxqUkmacvedkow re tno ErNtgwBtoosm ulrdozko ec gga pupd
xuya korruin.
Using Authenticator
OkHttp can automatically retry requests that fail due to lack of authentication.
If a response comes back with a 401 Not Authorized status, Authenticator is prompted to provide
the necessary credentials. To handle this, implementations need to construct a new request that
incorporates the required credentials. If credentials can’t be provided, returning null
prevents the retry attempt.
Pkimies he vtu medp xapnaeq ra miutk zew du orb DmvtVilqidzIqbatxigcom ezf Ootkapwolanor le
zios coxsektevb bowe.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
This section explains the authentication’s purpose and teaches about OkHttp
Interceptors and Authenticators.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.