使用Java在Azure中访问Office 365(SharePoint REST API)

问题描述:

我是Azure的新手,并且与Java进行SharePoint集成.

I am new to Azure and sharepoint integration with Java.

我正在尝试将Java与Sharepoint集成在一起.Sharepoint-Office 365在Azure ADFS中可用.我需要编写一个Java程序进行身份验证,然后使用Sharepoint提供的RESTful API访问文件.Azure正在使用WS-Federation身份验证过程.我一直在寻找可以帮助我使用WS-F身份验证然后访问文件的代码.我找不到任何有用的材料.

I am trying to integrate Java with Sharepoint. The Sharepoint-Office 365 is available in Azure ADFS. I need to write a Java program to authenticate and then access the files using the RESTful APIs provided by Sharepoint. Azure is using WS-Federation authentication process. I have been trying to look for the code that helps me to use the WS-F authentication and then access the files. I am not able to find any useful material.

基本身份验证不起作用,我也没有从事WS-F身份验证.因此,不知道从哪里开始.我还使用了 Office-365-sdk ,但是无法执行此操作,因为它使用的是客户端ID和与我的应用程序无关的其他属性.例如,由于共享点已经可用,因此不需要客户端ID.

The basic authentication is not working and I have nor worked in WS-F authentication. So no clue on where to start. I also used the Office-365-sdk to do this, but could not do it as it is using the client ID and other attributes which are not relevant to my application. For ex, the client ID is not required as the sharepoint is already available.

我得到了

I got this link as well, but some of the methods are missing and it does not explain about the libraries used to achieve. Please guide me in implementing this.

以下是

此服务中的方法应按以下方式调用:

Methods from this service should be called as follows:

package com.waveaccess.someproject.commons.service;

import com.google.common.base.Joiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Date;
import java.util.List;

/**
 * @author Maksim Kanev
 */
@Service
public class SharePointService {

    private static final Logger log = LoggerFactory.getLogger(SharePointService.class);

    @Autowired
    private SharePointServiceCached serviceCached;

    @Autowired
    private RestTemplate restTemplate;

    public String performHttpRequest(HttpMethod method, String path) throws Exception {
        Long executionDateTime = serviceCached.parseExecutionDateTime(new Date());
        String securityToken = serviceCached.receiveSecurityToken(executionDateTime);
        List<String> cookies = serviceCached.getSignInCookies(securityToken);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Cookie", Joiner.on(';').join(cookies));
        RequestEntity<String> requestEntity = new RequestEntity<>(headers, method, new URI(path));
        ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
        String responseBody = responseEntity.getBody();
        log.debug(responseBody);
        return responseBody;
    }

    public String performHttpRequest(String path, String json, boolean isUpdate, boolean isWithDigest) throws Exception {
        Long executionDateTime = serviceCached.parseExecutionDateTime(new Date());
        String securityToken = serviceCached.receiveSecurityToken(executionDateTime);
        List<String> cookies = serviceCached.getSignInCookies(securityToken);
        String formDigestValue = serviceCached.getFormDigestValue(cookies);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Cookie", Joiner.on(';').join(cookies));
        headers.add("Content-type", "application/json;odata=verbose");
        if (isWithDigest) {
            headers.add("X-RequestDigest", formDigestValue);
        }
        if (isUpdate) {
            headers.add("X-HTTP-Method", "MERGE");
            headers.add("IF-MATCH", "*");
        }
        RequestEntity<String> requestEntity = new RequestEntity<>(json, headers, HttpMethod.POST, new URI(path));
        ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
        String responseBody = responseEntity.getBody();
        log.debug(responseBody);
        return responseBody;
    }

    public String attachFile(String path, byte[] file) throws Exception {
        Long executionDateTime = serviceCached.parseExecutionDateTime(new Date());
        String securityToken = serviceCached.receiveSecurityToken(executionDateTime);
        List<String> cookies = serviceCached.getSignInCookies(securityToken);
        String formDigestValue = serviceCached.getFormDigestValue(cookies);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Cookie", Joiner.on(';').join(cookies));
        headers.add("X-RequestDigest", formDigestValue);
        headers.add("content-length", String.valueOf(file.length));
        RequestEntity<byte[]> requestEntity = new RequestEntity<>(file, headers, HttpMethod.POST, new URI(path));
        ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
        String responseBody = responseEntity.getBody();
        log.debug(responseBody);
        return responseBody;
    }

}

XPathExpressionFactoryBean 的配置:

package com.waveaccess.someproject.commons.config;

import com.waveaccess.someproject.commons.config.properties.SharePointProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.xml.xpath.XPathExpressionFactoryBean;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Maksim Kanev
 */
@Configuration
@EnableConfigurationProperties({SharePointProperties.class})
public class SharePointConfiguration {

    @Bean
    public XPathExpressionFactoryBean securityTokenExpressionFactoryBean() {
        XPathExpressionFactoryBean xPathExpressionFactoryBean = new XPathExpressionFactoryBean();
        xPathExpressionFactoryBean.setExpression("/S:Envelope/S:Body/wst:RequestSecurityTokenResponse/wst:RequestedSecurityToken/wsse:BinarySecurityToken");

        Map<String, String> namespaces = new HashMap<>();
        namespaces.put("S", "http://www.w3.org/2003/05/soap-envelope");
        namespaces.put("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        namespaces.put("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
        namespaces.put("wsa", "http://www.w3.org/2005/08/addressing");
        namespaces.put("wst", "http://schemas.xmlsoap.org/ws/2005/02/trust");
        xPathExpressionFactoryBean.setNamespaces(namespaces);
        return xPathExpressionFactoryBean;
    }
}

最后是 SharePointProperties :

package com.waveaccess.someproject.commons.config.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author Maksim Kanev
 */
@ConfigurationProperties("sharepoint")
public class SharePointProperties {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}