Forum Discussion

awitt's avatar
awitt
Guide
10 years ago

Unable to Authenticate - 401 Unauthorized

I've got two users, one an SSO user and one a non-SSO user.  The SSO user was created via our community page, and the non-SSO user created via the Lithium admin page.  Both are meant to be used for A...
  • nathan's avatar
    10 years ago

    The first thing I noticed is that you're making the call to the staging community, but you have not specified a basic authentication header.

     

    Most staging communities have basic authentication turned on by default. You see this in the browser when you first navigate to the site, and you get a little username/password pop-up. Browsers cache the username and password you specify, but when making calls via Java you will need to make sure that you include the appropriate header with every request.

     

    You'll need something like this (from this example):

    	// HTTP POST request
    	private void sendPost(String url) throws Exception {
    
    		URL obj = new URL(url);
    		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    		String encoded = Base64.encode("username:password");
    		con.setRequestProperty("Authorization", "Basic "+encoded);
    
    		con.setRequestMethod("POST");
    		// con.setRequestProperty("user.login", apiUsername);
    		// con.setRequestProperty("user.password", apiPassword);
    
    		...
    	}