Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phuongtailtranminh/ad0862bdddcc208bef7e030f4e62d36f to your computer and use it in GitHub Desktop.
Save phuongtailtranminh/ad0862bdddcc208bef7e030f4e62d36f to your computer and use it in GitHub Desktop.
Batch Request Assign Manager for User - Microsoft Graph API Java
package com.example.demo;
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.content.BatchRequestContent;
import com.microsoft.graph.content.BatchResponseContent;
import com.microsoft.graph.http.HttpMethod;
import com.microsoft.graph.logger.DefaultLogger;
import com.microsoft.graph.logger.LoggerLevel;
import com.microsoft.graph.options.HeaderOption;
import com.microsoft.graph.requests.GraphServiceClient;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@Slf4j
@SpringBootApplication
public class Application {
@Getter
@Setter
public static class ODataId {
@Expose
@SerializedName("@odata.id")
private String oData;
}
public static void main(String[] args) throws UnsupportedEncodingException {
final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId("YOUR_CLIENT_ID")
.clientSecret("YOUR_CLIENT_SECRET")
.tenantId("YOUR_TENANT_ID")
.build();
final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(
clientSecretCredential);
DefaultLogger logger = new DefaultLogger();
logger.setLoggingLevel(LoggerLevel.DEBUG);
final GraphServiceClient graphClient =
GraphServiceClient
.builder()
.logger(logger)
.authenticationProvider(tokenCredentialAuthProvider)
.buildClient();
// Create the batch request content with the steps
final BatchRequestContent batchRequestContent = new BatchRequestContent();
ODataId oDataId = new ODataId();
oDataId.setOData("https://graph.microsoft.com/v1.0/users/4c79648c-224d-4cfc-a2ea-610431bd2943");
batchRequestContent.addBatchRequestStep(
graphClient.users(encodeValue("AdeleV3#EXT#@YOUR_DOMAIN_COM"))
.manager().reference()
.buildRequest(new HeaderOption("Content-Type", "application/json")),
HttpMethod.PUT,
oDataId);
BatchResponseContent response = graphClient.batch().buildRequest().post(batchRequestContent);
log.info("{}", response.responses.get(0).status);
}
private static String encodeValue(String value) throws UnsupportedEncodingException {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment