Spring Boot:POST请求返回401 HttpUrlConnection

问题描述

我正在使用HttpURLConnection发送POST请求以获取访问令牌。但是,我得到的错误说 java.io.IOException:服务器返回HTTP响应代码:401,URL:https://xyz.auth0.com/oauth/token

注意:我可以通过邮递员获得访问令牌。

有人可以帮助我吗?预先感谢!

public String requestToken() throws Exception{

        StringBuilder response = new StringBuilder();

        URL url = new URL("https://xyz.auth0.com/oauth/token");
        //open a connection
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();

        //set the request method
        connection.setRequestMethod(TokenConstant.METHOD_POST);

        //set the request content-type header parameter
        connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        connection.setRequestProperty("charset","utf-8");

        //set response format type
        connection.setRequestProperty("Accept","application/json");
        connection.setDoOutput(true);

        //create request parameter
        String jsonInputString = "grant_type=client_credentials&client_id=xyz&client_secret=abc&audience=https://xyz.abc.com}";;
        // we need to write it
        try(OutputStream outputStream = connection.getOutputStream()){
            byte[] input = jsonInputString.getBytes("utf-8");
            outputStream.write(input,input.length);
        }

        //Read the response from Input Stream
        //get the input stream to read the response content
        try(BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        connection.getInputStream(),"utf-8"))){
            String responseLine = null;
            while((responseLine = br.readLine()) != null){
                response.append(responseLine.trim());
            }
        }
        return response.toString();
    }

解决方法

示例curl facebook oauth访问令牌生成(GET请求)-要生成应用访问令牌:

curl -X GET "https://graph.facebook.com/oauth/access_token
?client_id={your-app-id}
  &client_secret={your-app-secret}
  &grant_type=client_credentials"

注释了几个标题-不需要。将请求更改为GET。 注意:如果它可以很好地与curl配合使用,那么您的代码可能不需要修改就可以正常工作。

public static String requestToken() throws Exception{

    StringBuilder response = new StringBuilder();

    URL url = new URL("https://graph.facebook.com/oauth/access_token");
    //open a connection
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();

    //set the request method
    connection.setRequestMethod("GET");

    //set the request content-type header parameter
    // Commented - not required
    /*connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    connection.setRequestProperty("charset","utf-8");*/


    //set response format type
    connection.setRequestProperty("Accept","application/json");
    connection.setDoOutput(true);

    //create request parameter
    String jsonInputString = "client_id=<your-app-id>&client_secret=<your-app-secret>&grant_type=client_credentials";;
    // we need to write it
    try(OutputStream outputStream = connection.getOutputStream()){
        byte[] input = jsonInputString.getBytes("utf-8");
        outputStream.write(input,input.length);
    }

    //Read the response from Input Stream
    //get the input stream to read the response content
    try(BufferedReader br = new BufferedReader(
            new InputStreamReader(
                    connection.getInputStream(),"utf-8"))){
        String responseLine = null;
        while((responseLine = br.readLine()) != null){
            response.append(responseLine.trim());
        }
    }
    return response.toString();
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...