我想从 Make your first call in paypal 获取访问 token
我正在将 Curl 转换为 Kotlin 代码
这里我使用 retrofit 进行 api 调用。
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
我如何使用 kotlin 和 retofit 实现此目的?
最佳答案
interface PayPalClient {
@FormUrlEncoded
@POST("/v1/oauth2/token")
fun getAccessToken(
@Header("Authorization") credentials: String,
@Field("grant_type") grantType: String
): Single<PayPalAccessToken>}
主要 Activity
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)
}
关于android - 在 Android 中调用 Paypal 时无法获取访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52307322/