gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:21:45 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com