Httpclient get is returning not logged in

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

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...

 

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

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);

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?

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

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

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)?

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.

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

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?)

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.

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

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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Friday in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

309 views 11 10
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you