【发布时间】:2022-06-28 13:14:10
【问题描述】:
我正在尝试使用访问令牌创建一个文件夹 我获得了访问权限,但最终获得了响应代码 404。 我试图开发一个 JsonInputString 然后声明连接 我在 httpconn 中设置了值,然后尝试调用连接 我究竟做错了什么, 如果我对值进行硬编码,但当我动态获取它时,它会起作用。
这是我的代码
String siteURL, String folder) throws IOException {
String wsURL = siteURL + "/_api/web/folders";
URL url = new URL(wsURL);
URLConnection connection = url.openConnection();
String[] arr = siteURL.split("com");
String siteNameURL = arr[arr.length-1];
String jsonInputString = "{'__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/sites"+ siteNameURL +"/Shared%20Documents/"
+ folder + "'}";
HttpURLConnection httpConn = (HttpURLConnection) connection;
try {
clientID = sharePointclientID;
clientSecret = secretKey;
// AccessToken url : app.properties
accessTokenScr = getSharepointTokenScripting();
// Set header
httpConn.setRequestProperty("Content-Type", "application/json;odata=verbose");
httpConn.setRequestProperty("Accept", "application/json;odata=verbose");
httpConn.setRequestProperty("Authorization", "Bearer " + accessTokenScr);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("X-RequestDigest", "SHAREPOINT_FORM_DIGEST");
httpConn.setRequestProperty("Content-Length", Integer.toString(jsonInputString.getBytes("utf-8").length));
DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream());
byte[] input = jsonInputString.getBytes("utf-8");
wr.write(input, 0, input.length);
String responseStr = "";
if (httpConn.getResponseCode() == 201) {
responseStr = "Folder has been created successfully. ResponseCode: " + httpConn.getResponseCode();
} else {
responseStr += "Error while creating folder, ResponseCode: " + httpConn.getResponseCode() + " "
+ httpConn.getResponseMessage();
}
LOG.info(responseStr);
【问题讨论】:
标签: java spring sharepoint