微软认证

Microsoft authentication

我正在尝试使用 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 引导应用程序内部获取代码,因为我的应用程序不能有客户端输入(用于授权)这个?

或者还有其他方法可以使用 IGraphServiceClient 创建日历事件吗?

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