使用Java中的身份验证标头调用URL

时间:2018-08-09 15:24:23

标签: java http-headers


我本质上试图在Java中使用访问令牌调用URL失败。只使用终端我就可以做到没有任何问题

curl --request GET \
  --url 'https://api.typeform.com/forms/{form_id}/responses?since=2017-07-09T00%3A00%3A00&until=2017-07-10T00%3A00%3A00' \
  --header 'authorization: bearer {your_access_token}'

我用Java尝试过但没有成功:

try {
            URL u = new URL(resulturl2);
            c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setRequestProperty("authorization",apitoken);
            c.setRequestProperty("Content-length", "0");
            c.setUseCaches(false);
            //c.setAllowUserInteraction(false);
            c.setConnectTimeout(8000);
            c.setReadTimeout(8000);
            c.connect();
            int status = c.getResponseCode();
}catch(IOException e){

        }

任何建议都非常感谢!

1 个答案:

答案 0 :(得分:0)

我发现了错误。我需要将c.setRequestProperty("authorization",apitoken);更改为c.setRequestProperty("authorization","bearer "+apitoken);,并且有效