microsoft身份验证

i34xakig  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(555)

我正在尝试使用microsoft graph api,我需要使用该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启动应用程序内部获取代码,因为我的应用程序不能有客户端输入(用于auth)?
或者,是否有其他方法可以使用igraphserviceclient创建日历事件?

piok6c0g

piok6c0g1#

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

相关问题