Arp*_*shi 5 java spring-boot microsoft-graph-api
我正在尝试使用 microsoft graph api 获取和发布方法。Java代码:
IGraphServiceClient graphClient =
GraphServiceClient.builder().authenticationProvider(authProvider)
.buildClient();
graphClient.me().calendar().events().buildRequest().post(event);
User user = graphClient.me().buildRequest().get();
Run Code Online (Sandbox Code Playgroud)
在访问 api 时,我收到此错误:
线程“main”中出现异常 java.lang.NoSuchMethodError: okhttp3.Request$Builder.tag(Ljava/lang/Class;Ljava/lang/Object;)Lokhttp3/Request$Builder; 在 com.microsoft.graph.http.CoreHttpProvider.getHttpRequest(CoreHttpProvider.java:257) 在 com.microsoft.graph.http.CoreHttpProvider.sendRequestInternal(CoreHttpProvider.java:397) 在 com.microsoft.graph.http.CoreHttpProvider.send (CoreHttpProvider.java:220)在com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:200)在com.microsoft.graph.http.BaseRequest.send(BaseRequest.java:345)在com.microsoft。 graph.requests.extensions.EventRequest.post(EventRequest.java:135) at com.microsoft.graph.requests.extensions.EventCollectionRequest.post(EventCollectionRequest.java:75)
graph 和 graph-auth api 的 Maven 版本是:
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题吗?
这是主要方法:
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 = "abc@gmail.com";
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");
}
Run Code Online (Sandbox Code Playgroud)
看到异常 (NoSuchMethodError),您找到了该类,因此您的类路径中有一个 okhttp。我的猜测是使用了旧版本的 okhttp (可能是因为其他依赖项)并且您遇到了依赖项冲突。
当Java代码尝试调用类中不存在的方法时,会出现java.lang.NoSucMethodError,该方法可能是静态方法,也可能是非静态方法。
添加此依赖项并重试。
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
如果您的应用程序存在依赖项冲突,请在 pom.xml 中添加以下内容来解决它们:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
</dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)