【问题标题】:Microsoft authentication微软认证
【发布时间】:2021-01-04 10:38:15
【问题描述】:

我正在尝试使用 microsoft graph api,我需要授权码才能使用它。 在我的应用程序中无法将应用程序重定向到 Microsoft 登录站点。

我需要调用它,为此我需要 authProvider:

IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider)
            .buildClient();

我正在使用它来创建 authProvider:

UsernamePasswordProvider authProvider = new UsernamePasswordProvider(CLIENT_ID,
            Arrays.asList("https://graph.microsoft.com/user.read", "https://graph.microsoft.com/Mail.ReadWrite",
                    "https://graph.microsoft.com/Calendars.ReadWrite"),
            USERNAME, PASSWORD, NationalCloud.Global,
            TENANT, CLIENT_SECRET)

使用这个我得到错误:

  OAuthProblemException{error='invalid_grant', description='AADSTS65001: The user or administrator has not consented to use the application with ID 'e2bfebf6-cc77-49ec-82a3-28756ad377e5' named 'Milpitas Communications'. Send an interactive authorization request for this user and resource.

跟踪 ID:a2b91757-4849-4680-a089-001831ef7b00 相关 ID:ae894060-a2ce-444c-9889-96fd3cdfaea7

我也尝试使用它来创建 authprovider:

AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(CLIENT_ID,
            Arrays.asList("https://graph.microsoft.com/user.read", "https://graph.microsoft.com/Mail.ReadWrite",
                    "https://graph.microsoft.com/Calendars.ReadWrite"),
            AUTHORIZATION_CODE, REDIRECT_URL, NationalCloud.Global, "common",
            CLIENT_SECRET);

要运行上面我需要授权代码,谁能建议我如何在 Spring Boot 应用程序内部获取代码,因为我的应用程序不能有客户端输入(用于身份验证)?

或者是否有其他方法可以使用 IGraphServiceClient 来创建日历事件?

【问题讨论】:

  • 请查看documentation,然后试一试。
  • 您的用户是否启用了 mfa?您是以管理员身份登录的用户吗?

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


【解决方案1】:

Arrays.asList("https://graph.microsoft.com/user.read", "https://graph.microsoft.com/Mail.ReadWrite", "https://graph.microsoft.com/Calendars.ReadWrite") 替换为Arrays.asList("https://graph.microsoft.com/.default") 可以解决此问题。

我的代码供你参考:

public static void main(String[] args) {

    String USERNAME = "{username}";
    String PASSWORD = "{password}";
    String TENANT = "{tenantID}";
    String CLIENT_ID = "{clientID}";
    String CLIENT_SECRET = "{clientSecret}";

    UsernamePasswordProvider authProvider = new UsernamePasswordProvider(CLIENT_ID,
            Arrays.asList("https://graph.microsoft.com/.default"),
            USERNAME, PASSWORD, NationalCloud.Global,
            TENANT, CLIENT_SECRET);

    IGraphServiceClient graphClient = GraphServiceClient
            .builder()
            .authenticationProvider(authProvider)
            .buildClient();

    User user = graphClient.me().buildRequest().get();

    System.out.println(user.userPrincipalName);
}

【讨论】:

  • 如果我的回答对您有帮助,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充。)。见meta.stackexchange.com/questions/5234/…。这对其他社区成员可能是有益的。谢谢。
  • 感谢您的帮助。 invalid_grant 问题现已解决。我现在发布活动时遇到新错误。 java.lang.NoSuchMethodError: okhttp3.Request$Builder.tag(Ljava/lang/Class;Ljava/lang/Object;)Lokhttp3/Request$Builder;如果您有解决上述问题的任何想法,请提出建议。谢谢。
  • @ArpitJoshi 请分享您发布活动的完整代码。
猜你喜欢
  • 1970-01-01
  • 2022-12-05
  • 2021-12-11
  • 1970-01-01
  • 2019-05-30
  • 2017-12-24
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
相关资源
最近更新 更多