android - 在 Android 中调用 Paypal 时无法获取访问令牌
问题描述
我想从 Make your first call in paypal 获取访问令牌
我正在将 Curl 转换为 Kotlin 代码
在这里,我正在使用改造来进行 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>}
主要活动
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)
}
推荐阅读
- azure-devops - 如何使用 Azure DevOps Rest API 在发布中启动特定阶段
- python - 计算矩阵的 log2
- bash - 登录到 /var/log/ 时出现权限被拒绝错误
- python - ImporError:没有名为“win32com”Python 3.4 的模块
- apache-spark - Spark 任意状态流聚合,flatMapGroupsWithState API
- php - Laravel/PHP:如何验证渲染字段?
- android - 评估项目 ':app' 时出现问题
- .htaccess - 如何限制管理员 url 访问某些 IP 地址
- crashlytics - Crashlytics 与 SwiftyBeaver
- ios - 如何使用数组创建 NSPredicate 包含元素