none
Read a file from SharePoint using Java RRS feed

  • Question

  • Hi,

    I am trying to read a file from Sharepoint after authenticating it . But always i used to get error response as already connected. I am using below code inside submitToken method. Any help is appreciated.


    For authentication i have taken help from Dennis code which is present at:


    https://social.technet.microsoft.com/Forums/en-US/4c8d3696-1d41-44e3-897c-ac3c0b44f66a/share-point-online-authentication-using-java?referrer=https://social.technet.microsoft.com/Forums/en-US/4c8d3696-1d41-44e3-897c-ac3c0b44f66a/share-point-online-authentication-using-java?referrer=https://social.technet.microsoft.com/Forums/en-US/4c8d3696-1d41-44e3-897c-ac3c0b44f66a/share-point-online-authentication-using-java?referrer=https://social.technet.microsoft.com/Forums/en-US/4c8d3696-1d41-44e3-897c-ac3c0b44f66a/share-point-online-authentication-using-java?referrer=https://social.technet.microsoft.com/Forums/en-US/4c8d3696-1d41-44e3-897c-ac3c0b44f66a/share-point-online-authentication-using-java?referrer=https://social.technet.microsoft.com/Forums/en-US/4c8d3696-1d41-44e3-897c-ac3c0b44f66a/share-point-online-authentication-using-java?forum=sharepointsearch

    ERROR which i am getting is: 

    java.lang.IllegalStateException: Already connected
    at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty(Unknown Source)String

    Below is code 

    siteURL = "https://<tenant>.sharepoint.com/sites/<folder>" ;
    String wsUrl = siteURL + "/_api/web/GetFileByServerRelativeUrl(/Shared%20Documents//Files('Sample.txt')/$value";
    URL u1 = new URL(wsUrl);
    URLConnection uc1 = u1.openConnection();
    HttpURLConnection connection1 = (HttpURLConnection) uc1;
    connection1.setRequestMethod("GET");
    connection1.setRequestProperty("Cookie", headerName);

    connection1.setDoOutput(true);
    connection1.setDoInput(true);
    //System.out.println(token);
    connection1.getHeaderField("set-cookie");
    connection1.setRequestProperty("Authorization", "Bearer " + token);
    //connection1.setRequestProperty("Authorization"+"Bearer " + token +   headerName, "");
    // System.out.println(token);


    //Read the response
       String httpResponseStr = "";
       InputStreamReader isr = null;
       if (connection1.getResponseCode() == 200) {
         isr = new InputStreamReader(connection1.getInputStream());
         System.out.println("in"+isr);
       } else {
         isr = new InputStreamReader(connection1.getErrorStream());
         System.out.println("out"+isr);
       }
       BufferedReader in1 = new BufferedReader(isr); 
       String strLine = "";
       while ((strLine = in1.readLine()) != null) {
         httpResponseStr = httpResponseStr + strLine;
       }
       System.out.println("Response is"+httpResponseStr); //Print response

    Monday, October 29, 2018 11:26 AM

All replies

  • Hi,

    The following code for your reference:

    import java.io.File;
    
    import org.apache.commons.io.FileUtils;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.HttpStatus;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.NTCredentials;
    import org.apache.http.client.CredentialsProvider;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpHead;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.protocol.HttpClientContext;
    import org.apache.http.impl.client.BasicCredentialsProvider;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    
    public class HttpGetWithHttpClient {
    
      /**
       * download a file from a sharepoint library
       */
       public static void main(String[] args) throws Exception{
         CloseableHttpClient httpclient = HttpClients.custom()
          .setRetryHandler(new DefaultHttpRequestRetryHandler(0,false))
          .build();
    
    
         String user = "myusername";
         String pwd = "mypassword";
         CredentialsProvider credsProvider = new BasicCredentialsProvider();
         credsProvider.setCredentials(AuthScope.ANY,
            new NTCredentials(user, pwd, "", ""));
    
         // You may get 401 if you go through a load-balancer.
         // To fix this, go directly to one of the sharepoint web server or
         // change the config. See this article :
         // http://blog.crsw.com/2008/10/14/unauthorized-401-1-exception-calling-web-services-in-sharepoint/
         HttpHost target = new HttpHost("web.mysharepoint.local", 80, "http");
         HttpClientContext context = HttpClientContext.create();
         context.setCredentialsProvider(credsProvider);
    
         // The authentication is NTLM.
         // To trigger it, we send a minimal http request
         HttpHead request1 = new HttpHead("/");
         CloseableHttpResponse response1 = null;
         try {
           response1 = httpclient.execute(target, request1, context);
           EntityUtils.consume(response1.getEntity());
           System.out.println("1 : " + response1.getStatusLine().getStatusCode());
         }
         finally {
           if (response1 != null ) response1.close();
         }
    
         // The real request, reuse authentication
         String file = "/30500C/PubDoc/TEST/jira.log";  // source
         String targetFolder = "C:/TEMP";
         HttpGet request2 = new HttpGet("/_api/web/GetFileByServerRelativeUrl('" + file + "')/$value");
         CloseableHttpResponse response2 = null;
         try {
           response2 = httpclient.execute(target, request2, context);
           HttpEntity entity = response2.getEntity();
           int rc = response2.getStatusLine().getStatusCode();
           String reason = response2.getStatusLine().getReasonPhrase();
           if (rc == HttpStatus.SC_OK) {
              System.out.println("Writing "+ file + " to " + targetFolder);
              File f = new File(file);
              File ff= new File(targetFolder, f.getName());  // target
              // writing the byte array into a file using Apache Commons IO
              FileUtils.writeByteArrayToFile(ff, EntityUtils.toByteArray(entity));
           }
           else {
              throw new Exception("Problem while receiving " + file + "  reason : "
                  + reason + " httpcode : " + rc);
           }
         }
         finally {
           if (response2 != null) response2.close();
         }
         return;
       }
    }

    Reference:

    https://www.rgagnon.com/javadetails/java-get-document-sharepoint-library.html

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, October 30, 2018 8:24 AM
  • Thanks Jerry, BUt that didnt help. 

    Below  challenges i am facing.

    I have to read a file present at sharepoint location via java. But the problem is i am not able to fetch rtFa and fedAuth values from cookie.
    But same i am able to get from POSTMAN , and we need fedAuth value to proceed further and get digest value and which will help to read a file.
    Any help is appreciated

    If only token is passed in readFile I am getting below response.

    response :401
    outjava.io.InputStreamReader@3c756e4d
    Response is{"error_description":"Unsupported security token."}




    package sharepointTest;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.StringReader;
    import java.io.Writer;
    import java.net.CookieHandler;
    import java.net.CookieManager;
    import java.net.HttpCookie;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.List;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;

    import org.w3c.dom.Document;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;

    public class SharePointNewClass {

    public static void main(String [] args) throws XPathExpressionException, SAXException, ParserConfigurationException, IOException {
    System.out.println(new SharePointNewClass().login());
    }


    private final String sts = "https://login.microsoftonline.com/extSTS.srf";
    private final String login = "/_forms/default.aspx?wa=wsignin1.0";
    // SAML.xml from https://github.com/lstak/node-sharepoint
    private final String reqXML = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><s:Header><a:Action s:mustUnderstand=\"1\">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo><a:To s:mustUnderstand=\"1\">https://login.microsoftonline.com/extSTS.srf</a:To><o:Security s:mustUnderstand=\"1\" xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><o:UsernameToken><o:Username>[username]</o:Username><o:Password>[password]</o:Password></o:UsernameToken></o:Security></s:Header><s:Body><t:RequestSecurityToken xmlns:t=\"http://schemas.xmlsoap.org/ws/2005/02/trust\"><wsp:AppliesTo xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\"><a:EndpointReference><a:Address>[endpoint]</a:Address></a:EndpointReference></wsp:AppliesTo><t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType><t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType><t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType></t:RequestSecurityToken></s:Body></s:Envelope>";

    private String generateSAML() {
    String saml = reqXML
    .replace("[username]", "******************");
    saml = saml.replace("[password]", "*******");
    saml = saml.replace("[endpoint]", "https://<tenant>.sharepoint.com");
    return saml;
    }


    public String login() {
    String token = "";
    //String file;
    try {
    token = requestToken();

    String cookie = submitToken(token);
    }
    catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return token;

    }







    private String requestToken() throws XPathExpressionException, SAXException,
    ParserConfigurationException, IOException {

    String saml = generateSAML();

    URL u = new URL(sts);
    URLConnection uc = u.openConnection();
    HttpURLConnection connection = (HttpURLConnection) uc;

    //connection.connect();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.addRequestProperty("Content-Type","text/xml; charset=utf-8");

    OutputStream out = connection.getOutputStream();
    Writer wout = new OutputStreamWriter(out);
    wout.write(saml);

    wout.flush();
    wout.close();

    InputStream in = connection.getInputStream();
    int c;
    StringBuilder sb = new StringBuilder("");

    while ((c = in.read()) != -1)
    sb.append((char) (c));
    in.close();
    String result = sb.toString();

    System.out.println("Result :" + result);
    String token = extractToken(result);
    connection.disconnect();


    return token;
    }


    private String extractToken(String result) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
    // TODO Auto-generated method stub
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            Document document = db.parse(new InputSource(new StringReader(result)));

            XPathFactory xpf = XPathFactory.newInstance();
            XPath xp = xpf.newXPath();
            String token = xp.evaluate("//BinarySecurityToken/text()", document.getDocumentElement());

            return token;
    }

    private String submitToken(String token) throws IOException {
    // TODO Auto-generated method stub
    String url = "https://<tenant>.sharepoint.com/" + login;
    // CookieManager cookieManager = new CookieManager();
    // CookieHandler.setDefault(cookieManager);

    URL u = new URL(url);
    URLConnection uc = u.openConnection();
    HttpURLConnection connection = (HttpURLConnection) uc;
    connection.setDoOutput(true);

    connection.setRequestMethod("POST");
    //connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.addRequestProperty("Accept", "application/json; odata=verbose");
    connection.addRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)");
    connection.addRequestProperty("Content-Type","text/xml; charset=utf-8");
    connection.addRequestProperty("BinarySecurityToken", token);
    connection.setRequestProperty("Content-Length", "calcualte");


    OutputStream out = connection.getOutputStream();

    Writer wout = new OutputStreamWriter(out);

    wout.write(token);

    InputStream in = connection.getInputStream();
    //String headerName="";
    //String headerValue="";
    List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
    System.out.println("cookies:"+ cookies);

    //readFile(token);

    return "";
    }


    private void readFile(String token) {
    // TODO Auto-generated method stub
    String siteURL = "https://<tenant>.sharepoint.com/<folder>"  ;
    String wsUrl = siteURL + "/_api/web/GetFileByServerRelativeUrl(/Shared%20Documents//Files('Sample.txt')/$value" + login;

    try
    {
    URL u1 = new URL(wsUrl);


    URLConnection uc1 = u1.openConnection();
    HttpURLConnection connection1 = (HttpURLConnection) uc1;
    connection1.setRequestMethod("GET");


    connection1.setRequestProperty("Authorization", "Bearer " + token);
    System.out.println("response :"+connection1.getResponseCode());

    String httpResponseStr = "";
       InputStreamReader isr = null;
       if (connection1.getResponseCode() == 200) {
         isr = new InputStreamReader(connection1.getInputStream());
         System.out.println("in"+isr.toString());
       } else {
         isr = new InputStreamReader(connection1.getErrorStream());

         System.out.println("out"+isr.toString());
       }
       BufferedReader in1 = new BufferedReader(isr);
       String strLine = "";
       while ((strLine = in1.readLine()) != null) {
         httpResponseStr = httpResponseStr + strLine;
       }
       System.out.println("Response is"+httpResponseStr);

    }
    catch (Exception e)
    {
    e.printStackTrace();
    }

    }
    }




    O/p:
    Result :<?xml version="1.0" encoding="utf-8"?><S:Envelope xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Header><wsa:Action S:mustUnderstand="1" wsu:Id="Action">http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue</wsa:Action><wsa:To S:mustUnderstand="1" wsu:Id="To">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To><wsse:Security S:mustUnderstand="1"><wsu:Timestamp wsu:Id="TS" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsu:Created>2018-11-05T06:15:42.9392629Z</wsu:Created><wsu:Expires>2018-11-05T06:20:42.9392629Z</wsu:Expires></wsu:Timestamp></wsse:Security></S:Header><S:Body xmlns:S="http://www.w3.org/2003/05/soap-envelope"><wst:RequestSecurityTokenResponse xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust"><wst:TokenType>urn:passport:compact</wst:TokenType><wsp:AppliesTo><wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Address>https://tenant.sharepoint.com</wsa:Address></wsa:EndpointReference></wsp:AppliesTo><wst:Lifetime><wsu:Created>2018-11-05T06:15:42Z</wsu:Created><wsu:Expires>2018-11-06T06:15:42Z</wsu:Expires></wst:Lifetime><wst:RequestedSecurityToken><wsse:BinarySecurityToken Id="Compact0" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">t=TOKEN VALUE"></wsse:Reference></wsse:SecurityTokenReference></wst:RequestedUnattachedReference></wst:RequestSecurityTokenResponse></S:Body></S:Envelope>
    cookies:[stsservicecookie=ests; path=/; secure; HttpOnly, x-ms-gateway-slice=001; path=/; secure; HttpOnly, esctx=*******************************************-; domain=.login.microsoftonline.com; path=/; secure; HttpOnly, ExternalIdpStateHash=u2-TAnvuEVMlfA_1q9EZxCc8exkxh22SiKAPq8QqWgg; path=/; secure; HttpOnly, buid=******************-; expires=Wed, 05-Dec-2018 06:15:43 GMT; path=/; secure; HttpOnly]
    response :401
    outjava.io.InputStreamReader@3c756e4d
    Response is{"error_description":"Unsupported security token."}
    t=TOKEN ValUE

                 
    Monday, November 5, 2018 6:22 AM