Issue
I am trying to hit the microsoft graph api for get and post methods. Java code:
IGraphServiceClient graphClient =
GraphServiceClient.builder().authenticationProvider(authProvider)
.buildClient();
graphClient.me().calendar().events().buildRequest().post(event);
User user = graphClient.me().buildRequest().get();
On hitting the api I get this error:
Exception in thread "main" java.lang.NoSuchMethodError: okhttp3.Request$Builder.tag(Ljava/lang/Class;Ljava/lang/Object;)Lokhttp3/Request$Builder; at com.microsoft.graph.http.CoreHttpProvider.getHttpRequest(CoreHttpProvider.java:257) at com.microsoft.graph.http.CoreHttpProvider.sendRequestInternal(CoreHttpProvider.java:397) at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:220) at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:200) at com.microsoft.graph.http.BaseRequest.send(BaseRequest.java:345) at com.microsoft.graph.requests.extensions.EventRequest.post(EventRequest.java:135) at com.microsoft.graph.requests.extensions.EventCollectionRequest.post(EventCollectionRequest.java:75)
Maven versions for graph and graph-auth api's are:
<!-- https://mvnrepository.com/artifact/com.microsoft.graph/microsoft-graph -->
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>2.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.graph/microsoft-graph-auth -->
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph-auth</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>
Please help me resolve this issue?
This is the main method:
public static void main(String[] args) {
UsernamePasswordProvider authProvider = new UsernamePasswordProvider("{client_id}",
Arrays.asList("https://graph.microsoft.com/.default"),
"{username}", "{password}", NationalCloud.Global,
"{tenant_id}", "{client_secret}");
IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider)
.buildClient();
Event event = new Event();
event.subject = "Let's go for lunch";
ItemBody body = new ItemBody();
body.contentType = BodyType.HTML;
body.content = "Does mid month work for you?";
event.body = body;
DateTimeTimeZone start = new DateTimeTimeZone();
start.dateTime = "2021-01-15T12:00:00";
start.timeZone = "Pacific Standard Time";
event.start = start;
DateTimeTimeZone end = new DateTimeTimeZone();
end.dateTime = "2021-01-15T14:00:00";
end.timeZone = "Pacific Standard Time";
event.end = end;
Location location = new Location();
location.displayName = "";
event.location = location;
LinkedList<Attendee> attendeesList = new LinkedList<Attendee>();
Attendee attendees = new Attendee();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = "[email protected]";
emailAddress.name = "Abcd";
attendees.emailAddress = emailAddress;
attendees.type = AttendeeType.REQUIRED;
attendeesList.add(attendees);
event.attendees = attendeesList;
graphClient.me().calendar().events().buildRequest().post(event);
log.info("created the event");
}
Solution
Seeing the exception (NoSuchMethodError), you found the class, so you had a okhttp in your classpath. My guess is that an older version of okhttp was used (maybe because of other dependencies) and you got a dependency conflict.
java.lang.NoSucMethodError comes when Java code tries to call a method which is not existed on a class, this could be either static or non static method.
Add this dependency and try again.
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
If your application has dependency conflicts, then add this in your pom.xml to resolve them:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
</dependencies>
</dependencyManagement>
Answered By - Sibin