how to authenticate with MS graph using OnBehalfOfCredential method

Viewed 48

I need to connect to SharePoint in order to upload files to https://xxxxxxgroup.sharepoint.com/sites/xxx-xxxxxDepartment from a webapp I developed. I registered my app and all I have is:

  • tennantId
  • clientId
  • pemCertificate

so I'm using msgraph-sdk-java and as authentication OnBehalfOf Provider. below is my code:

public GraphServiceClient sharepointAuth() {
        private final static String CLIENT_ID = "xxxxxxxxx-xxxx-xxxxx-xxxx-xxxxxxxx";
        private final static String TENANT_ID = "xxxxxxxxx-xxxx-xxxxx-xxxx-xxxxxxxx";
        private final static List<String> SCOPES = Arrays.asList("xxxxx");
        private final static String CERTIFICATE_PATH = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        try {
            final OnBehalfOfCredential onBehalfOfCredential = new OnBehalfOfCredentialBuilder()
                .clientId(CLIENT_ID)
                .tenantId(TENANT_ID)
                .pemCertificate(CERTIFICATE_PATH)
                //.userAssertion()
                .build();

            final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(SCOPES, credential1);

            final GraphServiceClient graphClient = GraphServiceClient
                .builder()
                .authenticationProvider(tokenCredentialAuthProvider)
                .buildClient();

            System.out.println("------after auth------");

            final Drive result = graphClient
                .me()
                .drive()
                .buildRequest()
                .get();

            System.out.println("Found Drive " + result.id);
        } catch(Exception e) {
            System.out.println("exception");
            System.out.println(e);
        }
        return null;
    }

I'm not facing any error but I'm not able to get the drive, in logs:

------after auth------

I didn't find any example online, this is my 3rd full day working on it, I have many question:

  • pemCertificate: must be absolute/root path? (I tried both and nothing succeded)
  • userAssertion: is it mandatory? and how to fill it?
  • any example on how to connect to sharepoint? (if I only have tenantId, clienId and pemCertificate)

I appreciate your help!

0 Answers
Related