Cisco
Cisco

Reputation: 532

CAN I USE Basic Authentication to access SharePoint 2010 from JAVA with GET REST request?

I'm trying to get a list of data from SharePoint 2010 using REST request but I obtain this exception:

[err] java.io.IOException: Unauthorized

My code is:

public static String httpGet(String urlStr) throws IOException {

      URL url = new URL(urlStr);          
      String domain = "theuserdomain"; 
      String username ="myusername";
      String password = "mypassword";
      String credentials = domain+"\\"+username + ":" + password;
      String encoding = new sun.misc.BASE64Encoder().encode(credentials.getBytes());

      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestProperty("Authorization", "Basic " + encoding);
      conn.connect();

      if (conn.getResponseCode() != 200) {
        throw new IOException(conn.getResponseMessage());
      }

      // Buffer the result into a string
      BufferedReader rd = new BufferedReader(
          new InputStreamReader(conn.getInputStream()));
      StringBuilder sb = new StringBuilder();
      String line;
      while ((line = rd.readLine()) != null) {
        sb.append(line);
      }
      rd.close();

      conn.disconnect();
      return sb.toString();
    }

I think that my problem is that I don't set the user domain correctly...how can I set it on my request?

Upvotes: 3

Views: 2646

Answers (1)

Cisco
Cisco

Reputation: 532

RESOLVED using NTLM Authentication:

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(webPage);

        NTCredentials credentials = new NTCredentials(username, password, workstation, domain);


        httpClient.getCredentialsProvider().setCredentials(new AuthScope(server,port), credentials);

        HttpResponse response = httpClient.execute(getRequest);

Upvotes: 4

Related Questions