提问人:Pavel Lambo 提问时间:11/11/2023 最后编辑:Pavel Lambo 更新时间:11/11/2023 访问量:34
Apikey 对 POST 无效,但对 GET 请求有效?
Apikey is invalid for POST, but valid for GET requests?
问:
尝试从公共 api(谷歌翻译)获取任何响应。 在 POST 调用中有 403“请求缺少有效的 API 密钥”,但同时来自 GET 调用的成功响应。
val client = OkHttpClient()
// POST DETECT
val mediaType = "application/x-www-form-urlencoded".toMediaTypeOrNull()
val body = "q=English%20is%20hard%2C%20but%20detectably%20so".toRequestBody(mediaType)
val request = Request.Builder()
.url("$urlPrefix/detect")
.post(body)
.addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("Accept-Encoding", "application/gzip")
.addHeader("X-RapidAPI-Key", apiKey)
.addHeader("X-RapidAPI-Host", apiHost)
.build()
val response = client.newCall(request).execute()
println("POST: " + response.body!!.string())
// GET LANGUAGES
val reqGet = Request.Builder()
.url("$urlPrefix/languages")
.get()
.addHeader("Accept-Encoding", "application/gzip")
.addHeader("X-RapidAPI-Key", apiKey)
.addHeader("X-RapidAPI-Host", apiHost)
.build()
val respGet = client.newCall(reqGet).execute()
println("GET: " + respGet.body!!.string())
我应该使用 smth 而不是 okhttp3:okhttp:4.12.0 还是如何修复它?
可能 apiKey 是正确的,因为在 GET||POST 调用,响应为:
"{"message":"You are not subscribed to this API."}"
响应 POST:
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"errors": [
{
"message": "The request is missing a valid API key.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
请求标头:
{
"host": "google-translate1.p.rapidapi.com",
"content-type": "application/x-www-form-urlencoded; charset=utf-8",
"x-amzn-trace-id": "Root=1-654f490d-7f0a4fed6376983b7736b3d4",
"x-forwarded-port": "443",
"x-forwarded-proto": "https",
"content-length": "48",
"x-forwarded-for": "62.217.189.77",
"user-agent": "okhttp/4.12.0",
"x-rapidapi-host": "google-translate1.p.rapidapi.com",
"accept-encoding": "application/gzip"
}
答: 暂无答案
评论