android - 在 Android 中调用 Paypal 时无法获取访问 token

标签 android paypal kotlin retrofit2

我想从 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/

相关文章:

php - 向公众展示我的表格安全吗?

android - LazyColumn 内的 LazyHorizo​​ntalGrid

android - Android NDK 中的 FFMPEG - 无法加载库 "libavfilter.so"

android - 将 RecyclerView 放在 NestedScrollView 中,为所有项目调用 onBindView

paypal - 如何接受我网站上将提供给多个用户的服务的订阅付款

android - JSON异常 : No value for id after completing paypal payment

android - Kotlin 导航 fragment 中的 searchview.setonquerytextlistener() 类型不匹配

android - 将数据插入数据库时​​,我没有收到对 onChange 的更改

android - 从操作栏中删除 Logo 和文本并用图标替换它们

android - 在 Android 中使用 YouTube Data API v3 可恢复 YouTube 上传