【问题标题】:Error 403 when connecting to the official Clash Royale API连接到官方 Clash Royale API 时出现错误 403
【发布时间】:2018-08-18 13:32:40
【问题描述】:

我不断收到错误 403 无效授权,令牌完全正确。 还有其他人使用 Clash API 吗?到目前为止找不到任何可用的代码。

这是我用于测试请求的代码:

import java.io.*;
import java.net.*;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class Test {

    private static String token = "can't show that :)";

    public static void main(String[] args) {

        try {
            URL url = new URL("https://api.clashroyale.com/v1/locations");

            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            con.setRequestMethod("GET");
            con.setDoOutput(true);
            con.setRequestProperty("Authorization", "Bearer " + token);
            con.setRequestProperty("Accept", "application/json");

            InputStream content = (InputStream) con.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(content));

            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            JsonParser jp = new JsonParser();
            JsonElement je = jp.parse(in.readLine());
            String jsonString = gson.toJson(je);
            System.out.println(jsonString);

            con.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 您能准确地发布错误消息吗?
  • 这一行 con.setRequestProperty("Authorization", "Bearer " + token); 添加到 token 字符串。您确定令牌是正确的,但 "Bearer " + token 正确吗?
  • 这是我能想到它为什么不起作用的唯一原因,但它在官方文档中说,所以我假设
  • 这里是错误信息:java.io.IOException: Server returned HTTP response code: 403 for URL: api.clashroyale.com/v1/locations
  • 我觉得"Bearer" + token的base64编码不见了?

标签: java httprequest http-status-code-403


【解决方案1】:

你可以用你的实际令牌测试这个 curl 来检查它是否真的有效。

curl -X GET \
  https://api.royaleapi.com/v1/locations \
  -H 'Authorization: Bearer {{token}}' \
  -H 'Cache-Control: no-cache' 

您是否按照https://docs.royaleapi.com/#/authentication 上的说明获得了您的开发者令牌?

【讨论】:

    猜你喜欢
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 2022-01-24
    • 1970-01-01
    • 2017-08-18
    • 2020-07-10
    相关资源
    最近更新 更多