如何通过API在线验证到Sharepoint

时间:2019-04-29 10:54:36

标签: authentication sharepoint-online http-status-code-401

我想使用范围“客户端凭据”进行身份验证。我已经创建了基本代码以使用客户端ID和密码检索它。我可以获取令牌,但得到以下响应:

401-Unauthorized
{"error_description":"Invalid issuer or signature."}

我感觉到我得到的令牌是不正确的(也许我是从错误的URL获取令牌)。我使用的网址正确吗?

public class SharePointTest {

private static String URL_TOKEN = "https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/token";

private static String CLIENT_ID = "";
private static String CLIENT_SECRET="";
Private static String TOKEN="";

public String getToken(){
    try{
        String url = URL_TOKEN;
        String text = "client_id="+CLIENT_ID +
                        "&scope=https://graph.microsoft.com/.default" +
                            "&client_secret="+CLIENT_SECRET +
                                "&grant_type=client_credentials";
        System.out.println(url);
        URL u = new URL(url);
        URLConnection conn = u.openConnection();
        HttpURLConnection http = (HttpURLConnection) conn;
        http.setRequestMethod("POST");
        http.setDoOutput(true);
        http.setDoInput(true);
        http.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        http.addRequestProperty("Host", "login.microsoftonline.com");
        http.addRequestProperty("Accept", "application/xml");
        http.connect();

        OutputStream out = http.getOutputStream();
        if(!(send(out, text))){
            return null;
        }

        if(http.getResponseCode() == 200){
            InputStream in = http.getInputStream();
            TOKEN=read(in);
        }else{
            System.out.println(http.getResponseCode()+"-"+http.getResponseMessage());
            InputStream in = http.getErrorStream();
            System.out.println(read(in));
            return null;
        }

    }catch(JSONException e){
        System.out.println("SendJSON (JSONException):"+e.getMessage());
    }catch(MalformedURLException e) {
        System.out.println("SendJSON (MalformedURLException):"+e.getMessage());
    }catch(IOException e) {
        System.out.println("SendJSON (IOException):"+e.getMessage());
    }
    return null;
}

private String read (InputStream in){
    try {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];

        for (int n; (n = in.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }

        return out.toString();
    }catch(IOException e) {
        System.out.println("Read (IOException):"+e.getMessage());
    }
    return null;
}

private Boolean send(OutputStream out, String s) {
    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
        writer.write(s);
        writer.flush();
        writer.close();
        out.close();
        return true;
    }catch(IOException e) {
        System.out.println("Send (IOException):"+e.getMessage());
    }
    return false;
}

public static void main(String[] args){
    SharePointTest t = new SharePointTest();
    t.getToken();
}
}

以下是返回错误的函数(api调用):

public String getList() {

    try {
        String SITE = "https://<tenant>.sharepoint.com";
        String token = TOKEN;
        if(token != null){
            String url = SITE+URL_LIST;
            System.out.println(url);
            URL u = new URL(url);
            URLConnection conn = u.openConnection();
            HttpURLConnection http = (HttpURLConnection) conn;
            http.setRequestMethod("GET");
            http.setDoOutput(false);
            http.setDoInput(true);
            http.addRequestProperty("Authorization", "Bearer "+token);
            http.addRequestProperty("Accept", "application/json;odata=verbose");
            http.connect();

            if(http.getResponseCode() == 200){
                InputStream in = http.getInputStream();
                System.out.println(read(in));
            }else{
                System.out.println(http.getResponseCode()+"-"+http.getResponseMessage());
                InputStream in = http.getErrorStream();
                System.out.println(read(in));
                return null;
            }
        }
        System.out.println("no token");

    }catch(JSONException e){
        System.out.println("SendJSON (JSONException):"+e.getMessage());
    }catch(MalformedURLException e) {
        System.out.println("SendJSON (MalformedURLException):"+e.getMessage());
    }catch(IOException e) {
        System.out.println("SendJSON (IOException):"+e.getMessage());
    }
    return null;
}

0 个答案:

没有答案