【问题标题】:Getting error (java.lang.NoSuchMethodError: okhttp3.Request$Builder.tag) while posting event to microsoft graph将事件发布到 microsoft graph 时出现错误 (java.lang.NoSuchMethodError: okhttp3.Request$Builder.tag)
【发布时间】:2021-01-05 09:04:36
【问题描述】:

我正在尝试使用 microsoft graph api 获取 get 和 post 方法。 Java 代码:

IGraphServiceClient graphClient = 
             GraphServiceClient.builder().authenticationProvider(authProvider)
            .buildClient();
 graphClient.me().calendar().events().buildRequest().post(event);
 User user = graphClient.me().buildRequest().get();
 

点击 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) 在 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>

请帮我解决这个问题?

这是主要的方法:

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");

}

【问题讨论】:

  • 请分享您的main() 方法是怎样的。签名正确吗?喜欢public static void main(String [] args)
  • 修改问题添加main方法。

标签: java spring-boot microsoft-graph-api


【解决方案1】:

看到异常 (NoSuchMethodError),您找到了该类,因此您的类路径中有一个 okhttp。我的猜测是使用了旧版本的 okhttp(可能是因为其他依赖项)并且您遇到了依赖项冲突。

java.lang.NoSucMethodError 当 Java 代码尝试调用类中不存在的方法时出现,这可能是静态或非静态方法。

添加此依赖并重试。

 <dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.14.9</version>
</dependency>

如果您的应用程序存在依赖冲突,请将其添加到您的 pom.xml 来解决它们:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
      <version>3.14.9</version>
    </dependency>
  </dependencies>
</dependencyManagement>

【讨论】:

  • 已经试过了。不工作。我没有直接使用okhttp3。我正在调用 microsoft graph api,并且在某些时候该 api 正在使用 okhttp3。
  • 是的,正确!你能检查一下依赖树并找到okhttp的版本吗?。
  • 这是我使用依赖树找到的版本:com.squareup.okhttp3:okhttp:jar:3.9.0:compile
  • 我使用的是 Java 8
猜你喜欢
  • 2018-12-12
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 2019-06-19
相关资源
最近更新 更多