Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Httpclient get is returning not logged in

John Foster December 8, 2014

I am getting a error that I do not understand.   It looks like credentials are accepted. Here is my code.

    public static void main(String[] args) throws Exception
    {
//        GET  section
        DefaultHttpClient httpclient = new DefaultHttpClient();

        CredentialsProvider credentials = new BasicCredentialsProvider();
        credentials.setCredentials(
            new AuthScope(“<wiki-mine.com>”,<port>, “<realm>"),
            new UsernamePasswordCredentials(“<user>", “<password>"));

           HttpGet req = new HttpGet(“https://<wiki-mine.com>:<port>:8443/conflunce/rest/api/content/?title=Test+Page&spaceKey=~<user>");

        req.addHeader("accept", "application/json");
        req.setHeader("Content-Type", "application/json");
        final HttpResponse response = httpclient.execute(req);

        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode !=  200) {
            System.out.println("Request has failed, status code: " + statusCode);
            System.exit(-1);
        } else {
            final InputStream content = response.getEntity().getContent();
            System.out.println(IOUtils.toString(content));
            IOUtils.closeQuietly(content);
        }

This is the output:

 

{"errorMessage":"You are not logged in. <a href=\"/login.action\">Log in<\/a> here and try again.”}

 

Should this return the page?

 

thanks

 

 

 

1 answer

1 accepted

0 votes
Answer accepted
Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 8, 2014

Hi

2 things I have noticed... 

  • you have a typo in the url, unless your context path is really conflunce (and not confluence)

  • you have not set credentialsProvider on your httpclient

httpclient.setCredentialsProvider(credentials);

Hope it helps...

 

John Foster December 9, 2014

Hi I fixed the url, and added httpclient.setCredentialsProvider(credentials);. It still gives the same error. I think its is authentication problem because when I use a incorrect password it yields the same error. curl works fine and returns the page contain. Any ideas? Thanks

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 9, 2014

What if you just use the SeraphAuthentication params Confluence expects https://developer.atlassian.com/display/CONFDEV/HTTP+authentication+with+Seraph DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet req = new HttpGet("https://<wiki-mine.com>:<port>:8443/confluence/rest/api/content/?title=Test+Page&spaceKey=~<user>&os_username=<user>&os_password=<password>";); final HttpResponse response = httpclient.execute(req);

John Foster December 10, 2014

Thanks that works, but it is return html, not json. Even tho set the req header to request json with: req.addHeader("accept", "application/json"); req.setHeader("Content-Type", "application/json"); Is there a way to get json content as curl returns?

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 10, 2014

Just checked, it does nicely return application/json on our test server (no apache infront, just confluence tomcat) response.getEntity().getContentType() and it seems it does not matter if I set or not the above mentioned headers

John Foster December 10, 2014

Hum, its coming back as Content-Type: text/html;charset=UTF-8 for me. Could it be a difference in server setup?

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 11, 2014

which version of Confluence do you use (tried with 5.6.6 just now once again) and do you have some web server in-front of Confluence (like apache or something)?

John Foster December 11, 2014

We are running Confluence version 5.6.4. IT is checking to see if Apache is involved, but I think not. I checked to make sure the headers where indeed being set, but it does not matter if they are set or not.

John Foster December 15, 2014

IT confirmed that there is not wed server in-front of Confluence.

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 16, 2014

what version of HTTP commons you are using? Just very curious to understand the difference...tried with 4.3.2 trying to understand if there is an issue with redirect or something also, what do you see in Development Tools when you request the same URL via for example Chrome browser (what is the content type, response body?)

John Foster December 16, 2014

http commons 4.3.1. curl does not get a redirect it just sends back the correct json. firefox gets rediected to https://wiki.<domain>r.com:8443/dashboard.action, and displays the page. httpclient also goes to the dashboard, and returns the html for that page. also I can post a new page from curl, but when I do so from httpclient I get a 302 (found) or 200 if setRedirectStrategy(new LaxRedirectStrategy()); is called.

John Foster December 16, 2014

And the new page never shows up in confluence. I was using the same authentication in the curl as the post

Alex Medved _ConfiForms_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 16, 2014

frankly, I am completely lost, as if I do http://localhost:1990/confluence/rest/api/content/?title=More+information&spaceKey=ds&os_username=admin&os_password=admin on vanilla development version of Confluence it does nicely returns the application/json and the content. HTTP code is 200, no redirects Same goes when I run a java code provided above

John Foster December 17, 2014

thanks for all your help. I have it working now. It was a context path problem.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events