代币 import com.microsoft.aad.msal4j.*; 导入java.io.IOException; 导入java.net.MalformedURLException; 导入java.net.URI; 导入java.util.HashS

代币 import com.microsoft.aad.msal4j.*; 导入java.io.IOException; 导入java.net.MalformedURLException; 导入java.net.URI; 导入java.util.HashS,java,azure-active-directory,microsoft-graph-api,Java,Azure Active Directory,Microsoft Graph Api,代币 import com.microsoft.aad.msal4j.*; 导入java.io.IOException; 导入java.net.MalformedURLException; 导入java.net.URI; 导入java.util.HashSet; 导入java.util.Set; 导入java.util.concurrent.ExecutionException; 导入java.util.concurrent.ExecutorService; 导入java.util.concur

代币
import com.microsoft.aad.msal4j.*;
导入java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.URI;
导入java.util.HashSet;
导入java.util.Set;
导入java.util.concurrent.ExecutionException;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
公共类MsalTest{
公共静态void main(字符串[]args)引发异常{
Set scopes=new HashSet();
作用域。添加(“openid”);
scopes.add(“User.Read”);
String code=“授权码,AQABAI……UQ_CIAA”;
IClientCredential clientCredential=ClientCredentialFactory.create(ClassLoader.getSystemResourceAsStream(“./others/test.pfx”),“yourspassword”);
String clientId=“您的客户机id,dc17***-***-***-***-***-***-***-*****e56da5e7”;
字符串权限=”https://login.microsoftonline.com/+例如,tenantid:https://login.microsoftonline.com/e4c9ab4e-bd27-40d5-8459-230ba2a757fb";
URI redirectUri=新URI(“在azure ad中重定向应用程序的URI,https://localhost/");
IAAuthenticationResult=GetTokenWithCertificate(作用域、代码、clientCredential、clientId、权限、重定向URI);
System.out.println(result.accessToken());
}
静态IAAuthenticationResult GetTokenWithCertificate(设置作用域、字符串代码、IClientCredential clientCredential、字符串clientId、字符串权限、URI重定向URI){
IAAuthenticationResult=null;
ExecutorService=null;
试一试{
服务=Executors.newFixedThreadPool(1);
机密ClientApplication app=机密ClientApplication.builder(clientId,clientCredential).authority(authority).executorService(service).build();
AuthorizationCodeParameters AuthorizationCodeParameters=AuthorizationCodeParameters.builder(代码,重定向URI).scopes(scopes.build();
结果=app.acquireToken(authorizationCodeParameters.get();
}捕获(执行例外){
e、 printStackTrace();
}捕获(格式错误){
e、 printStackTrace();
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
service.shutdown();
}
返回结果;
}
}

您提到过吗?不幸的是,它只有ASP.NET的示例。请参阅了解如何使用rest API获取带有证书的令牌。注意,可能(也可能没有)有助于查找您要查找的内容是,该图不处理身份验证流。令牌生成完全由Azure Active Directory处理,Graph仅使用生成的令牌。请您有机会看看我的答案是否有用?
$cert=New-SelfSignedCertificate -Subject "CN=AADCert" -CertStoreLocation "Cert:\CurrentUser\My"  -KeyExportPolicy Exportable -KeySpec Signature
$bin = $cert.RawData
$base64Value = [System.Convert]::ToBase64String($bin)
$bin = $cert.GetCertHash()
$base64Thumbprint = [System.Convert]::ToBase64String($bin)
$cert | Export-Certificate -FilePath D:\test.cer
$CertPassword = ConvertTo-SecureString -String “YourPassword” -Force –AsPlainText
$cert | Export-PfxCertificate -FilePath D:\test.pfx -Password $CertPassword
import com.microsoft.aad.msal4j.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class MsalTest {
    public static void main(String[] args) throws Exception {

        Set<String> scopes = new HashSet<>();
        scopes.add("openid");
        scopes.add("User.Read");

        String code = "The authorization code, AQABAAI......UQ_CIAA";

        IClientCredential clientCredential = ClientCredentialFactory.create(ClassLoader.getSystemResourceAsStream("./others/test.pfx"), "YourPassword");

        String clientId = "Your client id, dc17****-****-****-****-****e56da5e7";

        String authority = "https://login.microsoftonline.com/+tenantid, for example: https://login.microsoftonline.com/e4c9ab4e-bd27-40d5-8459-230ba2a757fb";

        URI redirectUri = new URI("redirect uri of your applicaiton in azure ad, https://localhost/");

        IAuthenticationResult result = GetTokenWithCertficate(scopes, code, clientCredential, clientId, authority, redirectUri);

        System.out.println(result.accessToken());
    }

    static IAuthenticationResult GetTokenWithCertficate(Set<String> scopes, String code, IClientCredential clientCredential, String clientId, String authority, URI redirectUri){
        IAuthenticationResult result = null;
        ExecutorService service = null;
        try{
            service = Executors.newFixedThreadPool(1);
            ConfidentialClientApplication app = ConfidentialClientApplication.builder(clientId, clientCredential).authority(authority).executorService(service).build();
            AuthorizationCodeParameters authorizationCodeParameters = AuthorizationCodeParameters.builder(code, redirectUri).scopes(scopes).build();
            result = app.acquireToken(authorizationCodeParameters).get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            service.shutdown();
        }

        return result;
    }
}