403:使用Microsoft Graph

时间:2016-09-07 19:32:48

标签: java office365 adal microsoft-graph 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.

您能否告诉我使用哪个终点,以便我的应用可以在没有用户登录的情况下访问个别消息?我已经为应用程序提供了必要的所有权限(委托和应用程序权限)。

更新:这是我在http://jwt.calebb.net/上运行令牌时无法找到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

1 个答案:

答案 0 :(得分:1)

  

我已经为应用授予了必要的所有权限(委托和应用权限)。

这可能不是这种情况:)。通常403禁止意味着您的令牌不具备您所呼叫的API所需的范围(在scp声明中)。找到方便的简单方法:在调试器中获取令牌(它是一个很长的base64字符串),然后转到http://jwt.calebb.net/并粘贴它。你会看到它解码成JSON网页令牌。寻找roles声明:

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

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