如何使用 Microsoft Graph 进行身份验证和接收电子邮件

How to authenticate and get emails with Microsoft Graph

我正在寻找通过 Microsoft Graph 进行身份验证并在 Eclipse 中获取我所有电子邮件的解决方案 IDE。我用过'Client Credentials Provider'的认证方式。我对需要定义的 SCOPES 有疑问。请在下面找到错误和我的代码:

package JAVA_MicrosoftGraphAPI;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.logger.DefaultLogger;
import com.microsoft.graph.logger.LoggerLevel;
import com.microsoft.graph.models.User;
import com.microsoft.graph.requests.GraphServiceClient;
import okhttp3.Request;

public class YT_Video{

    private final static String CLIENT_ID = "CLIENT_ID";
    private final static String TENANT_ID = "TENANT_ID";
    private final static String SECRET_ID = "SECRET";

    //Set the scopes for your ms-graph request
    private final static List<String> SCOPES = Arrays.asList("User.Read", "Mail.Read", "openid", "offline_access", "profile");

    public static void main(String[] args) throws Exception {
        // Create the auth provider.        
        final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                .clientId(CLIENT_ID)
                .clientSecret(SECRET_ID)
                .tenantId(TENANT_ID)
                .build();

        
        final TokenCredentialAuthProvider tokenCredAuthProvider = new TokenCredentialAuthProvider(SCOPES, clientSecretCredential);
        System.out.println("First Step Reached. ");


        // Create default logger to only log errors
        DefaultLogger logger = new DefaultLogger();
        logger.setLoggingLevel(LoggerLevel.ERROR);


        // Build a Graph client
        GraphServiceClient<Request> graphClient = GraphServiceClient.builder()
                .authenticationProvider(tokenCredAuthProvider)
                .logger(logger)
                .buildClient();
        System.out.println("Second Step Reached. ");

        URL myUrl = new URL("https://graph.microsoft.com/v1.0/me/");
        final String accessToken = tokenCredAuthProvider.getAuthorizationTokenAsync(myUrl).get();
        System.out.println("Access token --> " + accessToken);

        // Just another optional step to get name of signed-in user.
        final User me = ((GraphServiceClient<Request>) graphClient).me().buildRequest().get();
        System.out.println("Hello " + me.displayName +  "( Synced !)");
        System.out.println("Hello " + me.mail +  "( Mail !)");
        System.out.println("Got " + me.messages.getCount() + " messages !");

    }

}





    at JAVA_MicrosoftGraphAPI.YT_Video.main(YT_Video.java:67)
Caused by: com.microsoft.aad.msal4j.MsalServiceException: AADSTS1002012: The provided value for scope User.Read openid profile offline_access Mail.Read is not valid. Client credential flows must have a scope value with /.default suffixed to the resource identifier (application ID URI).

您需要将范围更改为 https://graph.microsoft.com/.default

//Set the scopes for your ms-graph request
private final static List<String> SCOPES = Arrays.asList("https://graph.microsoft.com/.default");

User.ReadMail.Read 权限必须在 Azure 中的应用程序注册中预先配置,并且管理员必须事先同意这些权限。

资源:

Client credentials provider