This topic contains 1 reply, has 2 voices, and was last updated by  Peter Major 4 months ago.

  • Author
    Posts
  • #18443
     tommyqu1992 
    Participant

    Hi guys, I’m using two Java API calls for authentication and retrieve user data. The second API call will directly be executed after first API call with the token I get. However, everytime I authenticate the user, I will give me a new tokenID. And when I call the second API to retrieve user’s data, it will give me response exception.name=com.sun.identity.idsvcs.TokenExpired Cannot retrieve Token.

    
    	public Response authenticate(String username, String password) {
    		MediaType JSON = MediaType.parse("application/json");
    		RequestBody body = RequestBody.create(JSON, "{}");
    	    Request request = new Request.Builder()
    	    		.addHeader("Content-Type", "application/json")
    	    		.addHeader("X-OpenAM-Username", username)
    	    		.addHeader("X-OpenAM-Password", password)
    	            .url(BASE_URL + "/json/authenticate")
    	            .method("POST", body)
    	            .build();
    	    try {
    	    	Response response = client.newCall(request).execute();
    	    	String str = response.body().string();
    	    	JSONObject object = JSONObject.parseObject(str);
    	    	getUserProfile(object.getString("tokenId"));
    	    	return response;
    		} catch (Exception e) {
    			e.printStackTrace();
    			return null;
    		}
    	}
    
    	public void getUserProfile(String tokenId) {
    		System.out.println("Tokenid:"+tokenId);
    		MediaType JSON = MediaType.parse("application/json");
    		RequestBody body = RequestBody.create(JSON, "{}");
    	    Request request = new Request.Builder()
    	    		.addHeader("Content-Type", "application/json")
    	    		.addHeader("subjectid", tokenId)
    	            .url(BASE_URL + "/identity/attributes")
    	            .method("POST", body)
    	            .build();
    	    try {
    	    	Response response = client.newCall(request).execute();
    	    	System.out.println(response.body().string());
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    • This topic was modified 4 months ago by  Peter Major.
    #18444
     Peter Major 
    Moderator

    Where did you here about subjectid being an accepted header? The session ID can be either sent using a cookie, or as the header. The default cookie name is “iPlanetDirectoryPro”.
    Also don’t use the /identity/attributes endpoint, as it is legacy and planned to be removed in future versions of OpenAM. Instead try to use the /json/users/ endpoint.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

©2017 ForgeRock - we provide an identity and access platform to secure every online relationship for the enterprise market, educational sector and even entire countries. Click to view our privacy policy and terms of use.

Log in with your credentials

Forgot your details?