OKHTTP对POST请求返回401错误,但它正在与POSTMAN一起使用

时间:2018-10-02 07:11:13

标签: java post session-cookies okhttp3 http-status-code-401

我正在使用OKHTTP3向我的服务器代码发出POST请求。它返回401 UNAUTHORIZED错误。但是,同样的事情在POSTMAN上也适用。 POSTMAN和我的代码之间的唯一区别可能是会话cookie(我将其作为标头传递)。下面是我的代码

import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import okhttp3.*;
import org.json.JSONObject;

public class Main
{


  public static final String SESSION_URL = "http://localhost:8080/security/rest/token/access/session/0";
  public static final String CREATE_PROJECT_URL = "http://localhost:8080/managers/CDQRuntime/smartdq/training/";
  public static String PROJECT_NAME = "testproject201234";
  static String MANAGEMENT_TOKEN="YWRtaW46YWRtaW4=";
  public static void main(String[]args) throws IOException
  {
    //Get session api call
    Request request = new Request.Builder()
            .url(SESSION_URL)
            .get()
            .addHeader("Authorization", "Basic " + MANAGEMENT_TOKEN)
            .build();
    OkHttpClient client=new OkHttpClient();
    Response response = client.newCall(request).execute();
    String serverResponse = response.body().string();
    System.out.println(serverResponse);
    JSONObject responseMap = new JSONObject(serverResponse);
    System.out.println(responseMap);
    String accessToken=(String)responseMap.get("access_token");
    System.out.println(accessToken);




    //Create Project api call
    JSONObject createProjectCallBody = new JSONObject();
    MediaType JSON = MediaType.parse("application/json");
    createProjectCallBody.put("projectState","Initial");
    createProjectCallBody.put("projectName", PROJECT_NAME);
    createProjectCallBody.put("projectDescription","this is a test");
    RequestBody body = RequestBody.create(JSON, createProjectCallBody.toString());
        request = new Request.Builder()
            .url(CREATE_PROJECT_URL)
            .post(body)
            .addHeader("Authorization","Bearer "+accessToken)
            .addHeader("Content-Type","application/json").
                addHeader("SESSION", responseMap.get("session").toString())
            .build();
        System.out.println(request.headers().toString());

    client=new OkHttpClient();

    response = client.newCall(request).execute();
    System.out.println(response);




  }

  private static Map<String, Object> getStringObjectMap(String content)
  {
    return new Gson().fromJson(
              content, new TypeToken<HashMap<String, Object>>() {}.getType()
      );
  }

}

下面是工作邮递员响应的链接

authoriation and other headers in POSTMAN request 这是邮递员的尸体 Body sent in the POSTMAN request

在各种Sys out之后,下面是控制台日志,该日志显示代码在第二次api调用上失败,从第一个响应中提取的令牌是正确的

{"access_token":"eyJjdHkiOiJKV1QiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiZGlyIn0..LppdczJz8iUazAT1MXF7wg.vu6sPxXGhvWUjujS8u67zfYSF9zWWXibsk8iyiDag3kXT63Q_fMVusPyIJWabbS-Aa3OS_jcWMI8veKu2kIKC_fTGn2VjfU4Rs4Gt4L95PnJnmeH11AxJi2z9Lq_dw35hVWQBWJZjhucihvb7A5p0z7vroa-5boeXHUJPg1qcfL_9S8ab91hIy2aBry4h_QSqOXp1hD22pE7KcrRFnbc3JoDhnoL9iBZQXXiUXyw9Cvce8krIkeD7_aZCIF7NVz66xZ9WKly3-OQDvR94EG4rTwAPAo8z2Zc6ioQSSdQWvXhij5V97Pjk4UPjHBP1kJxhC2y-aPfMBjnWZ0lhXtb7CfSsDwVKUl7egczsaL4-CQ3a_u9ptpDoXwXvQCTLChRbGkxaV4Lmhm7jbwaHo00xHpUP2XlPnaj7wNtqWDxZwJOUk5GVy4qGlnqpMhyJ-0YONcBxSOIcxSSZGR95h_ceiMHF4f2XDmosv-iIBvl8NuX_VC_atlsJ1_6DRPCFDR-X-p3oKWaLgwtH1JGv5F-bo9SeM6EeV9AE5PEweabiN9UtI72zuQKtiCa-1p690yWqK7F-CmZTNij4EZV7knleHoDdQ3SZhNPFnuybkq-0v8JJM2bPlkfzmxhSLIOzY87BcynHIVBPcXFt5FvBvFvzSKd1nhYMynfzuOsrq90Q3f-6k9WKvHAYhpJKNAkhGNWs11jgHAUrPS85Yub3kLEHMICunOKtVgwTSsjmkpMfd6QskIpdc17WWzCXg7XiKeCzNFyDpP6KNzQ0mHqw59ou0XMVByLZwDAeZIe00ix3kqVyI3o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be22845dfd

Response{protocol=http/1.1, code=401, message=Unauthorized, url=http://localhost:8080/managers/CDQRuntime/smartdq/training/}

0 个答案:

没有答案