【问题标题】:403:forbidden when reading mails from server side application using Microsoft Graph403:使用 Microsoft Graph 从服务器端应用程序读取邮件时被禁止
【发布时间】:2016-09-07 19:32:48
【问题描述】:

我正在开发一个服务器端应用程序来从我们的 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

【问题讨论】:

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


    【解决方案1】:

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

    这可能不是这样的:)。通常 403 禁止意味着您的令牌没有您调用的 API 所需的范围(在 scp 声明中)。查找的简单方法:在调试器中获取令牌(它是一个很大的长 base64 字符串),然后转到 http://jwt.calebb.net/ 并将其粘贴。您会看到它被解码为 JSON Web 令牌。查找roles 声明:

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

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

    【讨论】:

    • 根本没有 scp 部分。 :(
    • 我更新了我的问题以反映当前令牌。
    • 范围和上面的不一样,应该在哪里配置?
    • 您同意申请了吗?您需要导航到 - https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id={0}&resource={1}&redirect_uri={2}&state=SomeState&prompt=admin_consent(确保替换 { 0}、{1} 和 {2})
    • 我已使用 Azure 管理界面为应用程序授予应用程序和委托权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多