【问题标题】:Can't get access token when call in Paypal in Android在 Android 中调用 Paypal 时无法获取访问令牌
【发布时间】:2019-02-17 19:29:10
【问题描述】:

我想从 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 来实现这一点?

【问题讨论】:

    标签: android paypal kotlin retrofit2


    【解决方案1】:
    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)
    }
    

    【讨论】:

    • 我们如何将 client_id 和 client_sceret 添加为单个字符串?那需要什么格式吗?在邮递员中,我们添加为基本身份验证?
    • val credentials = Credentials.basic(CLIENT_ID, CLIENT_SECRET)
    猜你喜欢
    • 2014-11-10
    • 2017-07-30
    • 2021-11-01
    • 2020-01-06
    • 2017-03-22
    • 2023-03-13
    • 2022-01-01
    相关资源
    最近更新 更多