java - 403 :forbidden when reading mails from server side application using Microsoft Graph

标签 java office365 adal microsoft-graph-api azure-ad-graph-api

我正在开发一个服务器端应用程序,用于从 Office 365 检索电子邮件、过滤电子邮件并将其存储在我们的数据库中,而无需用户登录。如果您想查看一些详细信息,Here 是我之前问题的链接。

我有以下方法

@Test
    public void testGetAccessTokenDeamon() throws Exception {

        String tenant="f0245-fd24-r3s-be8a-7745gra60be3";
        String authority = "https://login.windows.net/"+tenant+"/oauth2/authorize";
        ExecutorService service=null;
        service= Executors.newFixedThreadPool(1);

        try{

            AuthenticationContext authenticationContext= new AuthenticationContext(authority,false,service);
            String certFile="/mycert.pfx";
            InputStream pkcs12Cert= new SharedFileInputStream(certFile);

            AsymmetricKeyCredential credential=AsymmetricKeyCredential.create("g564f4-e53c-45b7-938a-gt6445gy667",pkcs12Cert,"passwd");


            Future<AuthenticationResult> future=authenticationContext.acquireToken("https://graph.microsoft.com",credential,null);

            System.out.println("Token Received"+future.get().getAccessToken());
            String token = future.get().getAccessToken();

            HttpGet httpGet = new HttpGet("https://graph.microsoft.com/v1.0/users");

            httpGet.setHeader("Authorization", "Bearer "+token);


            GraphServices graphServices = new GraphServices();
            ResponseEntity<String> responseEntity;


            responseEntity = graphServices.getEmails(token);




            //HttpClient httpClient= HttpClients.createDefault();

            //HttpResponse response=httpClient.execute(httpGet);
            //HttpEntity entity=response.getEntity();

        }
        catch (MalformedURLException e){
            e.printStackTrace();

        }
        catch (Exception e){
            e.printStackTrace();
        }


    }

public ResponseEntity<String> getEmails(String accessToken) throws Exception
    {
        logger.info("In getEmails");

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> request;

        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add(AuthorizationConstants.AUTHORIZATION, AuthorizationConstants.BEARER + " " + accessToken);
        request = new HttpEntity<String>(headers);

        ResponseEntity<String> response = restTemplate.exchange("https://graph.microsoft.com/v1.0/users/abc@microsoft.com/messages", HttpMethod.GET, request, String.class);
        return response;
    }

如果我尝试在restTemplate中使用 https://graph.microsoft.com/v1.0/users/ ,我将获得用户列表,但对于特定用户,上述代码将返回403。

您能否建议我使用哪些端点,以便我的应用程序无需用户登录即可访问个人消息?我已经向该应用授予了所有必要的权限(委托(delegate)和应用权限)。

更新:这是我在 http://jwt.calebb.net/ 上运行 token 但找不到 scp/scope 部分时得到的结果。

{
 typ: "JWT",
 alg: "RS256",
 x5t: "xxxxx_mfg5JKHrwLBbd_4s",
 kid: "xxxxx_mfg5JKHrwLBbd_4s"
}.
{
 aud: "https://graph.microsoft.com",
 iss: "https://sts.windows.net/f456fyu44-df44-d3t3-f342-66423er4323/",
 iat: 1473280446,
 nbf: 1473280446,
 exp: 1473284346,
 appid: "4fw423gh-er423-45b7-zd32-3fwer2343szd",
 appidacr: "2",
 e_exp: 10800,
 idp: "https://sts.windows.net/g0412253-aeb2-4a8a-ju76-8736272a3u7e3/",
 oid: "33dd3455-0195-4708-rt44-34552321ds32d2",
 sub: "33dd3455-0195-4708-813d-34552321ds32d2",
 tid: "33dd3455-ae23-r456-be8a-7737cf4321d2e3",
 ver: "1.0"
}.
[signature]

以下是我为该应用授予的权限(目前为所有权限)。

Microsoft Graph - 应用程序权限:18 - 委派权限:40 Windows Azure Active Directory - 应用程序权限:4 - 委派权限:8 Office 365 Exchange Online -应用程序权限:9 - 委派权限:30

最佳答案

I've already gave the app all permissions (delegate and app permissions) that are necessary.

情况可能并非如此:)。通常,403 禁止表示您的 token 不具备您调用的 API 所需的范围(在 scp 声明中)。简单的查找方法:在调试器中获取 token (它是一个很大的长 Base64 字符串),然后转到 http://jwt.calebb.net/并将其粘贴进去。您将看到它被解码为 JSON Web token 。查找 roles 声明:

roles: [
  "Calendars.Read",
  "Mail.Read",
  "Contacts.Read" 
],

如果您没有 Mail.ReadMail.ReadWrite,则您没有配置必要的权限。

关于java - 403 :forbidden when reading mails from server side application using Microsoft Graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39377739/

相关文章:

Azure 条件访问不适用于 token 获取请求?

java - Spring Webflux : Extract a value from a Mono and save it into another variable

charts - POWER BI - 根据值显示/隐藏图表

azure - 如何配置 ADALv3 以支持用户 ID 之间的切换...类似于 Gmail?

office365 - Office 365 API 定期 session 并不总是返回

c# - 如何在 asp.net C# 中使用启用两因素身份验证的 office365 电子邮件帐户发送电子邮件?

java - ADAL4J 缓存支持缺失?

java - Google 身份验证找不到 .p12 文件

java - LibGDX 多相机

java - C++ 中的 "derived"与 Java 中的 "extended"是一回事吗?