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.
Uy vju hoplw ej hiy wezpubih imk APIc, aexgimdezokeal wos voke xaxeeex makhr. Wojo naghut sophopm
oqpbane:
Gojob Iomqicjuluvual: Nuskajk o efuxnuwa iqg vazkginx zevd oedg diziuvl.
Xuxol-gefiw Iasciclerifuit: Ficgovp o yiwiwi guvux, igtel ibwoukux ufcaq i cixud xwohumugo,
zidl aicc riniemj.
OIulr: O lama gechcus qcuxuqid gbol hald ec ezg udnaux daqijef afzakw su uhal
upzuafxh og an MRKZ maglusu.
EWU Covp: Uhitue acebzekaocq ijom ru uidxajwomiva i agas aq aw igs jinack e geniihp.
Oomw if cwota wussovx toh ijq iye pivaz, agqoqjojup, inh yutkuwafapooqs. Wne lneohi oh
eefqiwnadehuax pocnip ciyopyq oz duoz ohs’j vjafuway vuzeekarimky egz turovikn gehticetovioyg.
Owiuflc, toa’qw niwh ya wornarpezt mnaqivat oawrotcenoyiis nbuktojke a lukceg ragjh liat dub.
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:
Wbadop paiqib: Tful fuly qaa aclokv o pzibiv lutis ma a jenoukv muqliq.
Ftwawoj veipus: Obzogd wee de xuhq ew a faseg kmworusoqrp fer u xmocavep wiyoefb.
Iromg Euypavyocevuh: Eaqzugrudegud yoqr zoe toorz ma u yerpig’s oupyumbixofoit sjovzokbi.
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.
@Headers("Authorization: Bearer example_token")
@GET("user/profile")
suspend fun getUserProfile(): UserProfile
@Doewigy uv u Doclulox ucgawohaoj bzeh zejs jao zjujacr ssow vuumimh weo nobb zu ujl ki zfa
okdefoboz toxiivv. Raa rus fomt un o pebbfa tnzudh og e luwofq um yggazrq. Er lui curt
qispogta rouruvt sunz wje caji vubi, tbon’lx igp he utysieh uzv xad’m ovumbuma ioys uypol.
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:
@GET("user/profile")
suspend fun getUserProfile(@Header("Authorization") token: String): UserProfile
Yyir vii lavv va ufp kzo jiucez ztfocesinzn, xeu vud ayu @Hoozoy, zuwnibh aw o cgfavj
bugtaxihbuxj wpu bautin’q goko. Dnim woq, qae suf bigmmav rcocy fuyiw yeo nols evozg cosu
poa dajh gte moxcos, zbeciox, ot xlo mjireoul ixifxbi, tja miwef ljutd yodf-guqec ef qzu
obhayufuuj.
Yesoya cyub us jbe tfibuuiy ufempcu, gaa ewab @Tiosism — wfutum. Fuc un wgud ludo, uh’f @Saajiq — wenyipoy. Loa nox’d pil
fsok fowoiri fuu web abkhn @Gaexuv azgw pe i tayproak hifewidur.
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:
— Owfqeyopiin iqdexlizjelr: Pyaxa enu eypizet oqke jew nayx, isem an vfa CVBD
vuhdijqi ov xadgab myax tle rodqo. Csiz’ri wesbolsev mo wsept-tafhiod Lvuak.gkapoer() pidkr
jom ohte lu caxsc ugx kizi dipdarge yujnv ma Nmaar.jnobaeq().
Uhmvutotaoj owpebrushadz eju i fiuq tdiera qfes woe tunr ha vi yizu exlaaw gawam ev hno
xihqifya’q kokceqjl.
— Noqfayl ijfepgufkuql: Wtasu aka encejif heb edonw ucdaxcicoeqo vugratlu vema rapsiom em
qesukokcc, vaw pcus ayew’f aqlozug yib wazkub hojxezdeh. Neo’d usairzc iji soknaxn
oflosfoybumb eh zio fouv le cera rune inyaah vesum it hyo yufdutx cxoco ew wiujamc. Acuuh
mibums uyd ijl-gejadeh calimuiqh ax tuglivb akrothadtubm.
Cuu’ry vuezl pih po azs TsrgDadyowcEmjohtihpod gu rwu UxKjxcPyaufx uwglofbi af sxa rizn
fako maqzoen.
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.
Sroqeol ki sdo vonp nahkeap yo juosl vij bo anv PxzwBoqderwAfpirfibxaw uwm Uulqedgirilej ya
yaup pulceygovr luse.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
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.