OkHttp & OAuth: Token Refreshes

2018-12-13 17:58

Every time we log into an app using our Facebook or Google account, we rely on the authentication protocol OAuth.

As developers, we frequently have to work with APIs that use OAuth as their authentication mechanism. The Coinbase App uses OAuth to authenticate users so they can buy and sell digital currencies with the Coinbase API.

“Highly accurate depiction of OAuth authentication”

After receiving a token, apps typically persist it and apply it to each request that requires authentication. In the Coinbase Android app, we do this using an Interceptor. Each request asks an AccessTokenProvider for the token and tacks it onto its headers.

https://medium.com/media/8709548e86a7c02633212e92a44d5cde/hrefhttps://medium.com/media/00392c5855e81530ed33eab388569926/href

But what happens when our access token expires or gets revoked? Making a request with an invalid token results in a 401. Wouldn’t it be nice if our network stack could handle refreshing our token automatically and even retry failed requests for callers?

Enter OkHttp’s Authenticator API. Using a custom Authenticator we can build this behavior into OkHttp.

https://medium.com/media/55f60e7892c56705364c5786c282cf62/href

Now when building our OkHttpClient we plug our Authenticator in:

OkHttpClient.Builder()
.authenticator(AccessTokenAuthenticator(accessTokenProvider))
.build()

Eureka! Instead of having to retry 401s and perform refreshes in our application code, we’ve built this behavior into OkHttp for all our authenticated calls.

Special thanks to Tristan Waddington for being a sounding board for OAuth and OkHttp internals.

P.S. Coinbase is hiring!

OkHttp & OAuth: Token Refreshes was originally published in The Coinbase Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.

Similar to Notcoin - Blum - Airdrops In 2024

origin »

Waves Community Token (WCT) на Currencies.ru

$ 0 (+0.00%)
Объем 24H $0
Изменеия 24h: 0.00 %, 7d: 1.05 %
Cегодня L: $0 - H: $0
Капитализация $0 Rank 99999
Цена в час новости $ 0.3246 (-100%)

coinbase medium careers working token oauth refreshes

coinbase medium → Результатов: 126