【问题标题】:How can I form a correct query?如何形成正确的查询?
【发布时间】:2021-11-19 18:34:48
【问题描述】:

我有下一个文档:

要创建帐户访问同意,您需要通过 识别您的应用程序的访问令牌:只需替换 CLIENT_ID 和 CLIENT_SECRET 在下面的 curl 中检索一个有效的:

curl -k -X POST \
  https://mandarine.sai.com/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&scope=accounts'

回复:

HTTP/1.1 200
status: 200
content-type: application/json; charset=utf-8
 
{
    "token_type": "Bearer",
    "access_token": "Ci5A6huK2OD0d6...kVwLE9FuHwHGCf6xo5",
    "expires_in": 7200,
    "scope": "accounts"
}

所以我尝试在我的 ApiInterface 中进行下一步:

@Headers(
    "Accept: application/json",
    "Content-Type: application/x-www-form-urlencoded"
)
@POST("/token")
suspend fun requestToken(
    @Query("client_ id") clientId: String,
    @Query("client_secret") clientSecret: String
): AccessTokenResponse

但我不确定我是否为我的请求正确填写了data,即不清楚我仍应将client_credentialsscope 放在哪里

【问题讨论】:

    标签: android post request retrofit


    【解决方案1】:

    对于这个请求,需要编写如下请求:

    @Headers(
        "Accept: application/json",
        "Content-Type: application/x-www-form-urlencoded"
    )
    @POST("/token")
    suspend fun requestToken(
        @Query("grant_type") grantType: String,
        @Query("client_id") clientId: String,
        @Query("client_secret") clientSecret: String,
        @Query("scope") scope: String,
    ): AccessTokenResponse
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 2019-12-20
      相关资源
      最近更新 更多