interface PayPalClient {
@FormUrlEncoded
@POST("/v1/oauth2/token")
fun getAccessToken(
@Header("Authorization") credentials: String,
@Field("grant_type") grantType: String
): Single<PayPalAccessToken>}
MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val credentials = Credentials.basic(CLIENT_ID, CLIENT_SECRET)
initRetrofit().getAccessToken(credentials, "client_credentials")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ token -> Log.d(TAG, token.accessToken) },
{ error -> Log.d(TAG, error.localizedMessage) }
)
}
private fun initRetrofit(): PayPalClient {
return Retrofit.Builder()
.baseUrl("https://api.sandbox.paypal.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build().create(PayPalClient::class.java)
}