我有以下 Android 代碼
@POST("/api/oauth/token") Call<Token> login(
@Query("client_id") String clientId,
@Query("client_secret") String clientSecret,
@Query("redirect_uri") String redirectUrl,
@Body() AuthBody authBody
)
import com.google.gson.annotations.SerializedName
data class AuthBody(
@SerializedName("username") var username: String?,
@SerializedName("password") var password: String?,
@SerializedName("grant_type") var grantType: String = "password"
)
對于發送請求,他們使用
import retrofit2.Call;
此代碼用于從后端服務器獲取訪問令牌。我想將其轉換為 Curl 命令,這樣我就可以在不運行此代碼的情況下發送請求。
到目前為止我所擁有的是
curl "https://example.com/api/oauth/token?client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}" --data '{"username":"{my-username}","password":"{my-password}","grant_type":"password"}'
但是在運行時,服務器回應是
{"error":"invalid_request","error_description":"translation missing: en.doorkeeper.errors.messages.invalid_request.missing_param"}
我不知道我在這里缺少什么,請幫助我。
uj5u.com熱心網友回復:
我找到了。
我上面的 curl 命令是正確的,但是,這還不夠。Curlcontent-type: application/x-www-form-urlencoded默認發送標頭。但由于我們正在發送 json 資料。我們需要將其設定為content-type: application/json.
所以作業命令是
curl "https://example.com/api/oauth/token?client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}" --data '{"username":"{my-username}","password":"{my-password}","grant_type":"password"} -H "content-type: application/json"'
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/392915.html
上一篇:如何填寫表單并將其提交到不同的域
