微软图形 API Authentication_MissingOrMalformed

Microsoft Graph API Authentication_MissingOrMalformed

我正在使用 oauth2/token 来验证我的应用程序并获得 access_token。下面是工作正常的 java 代码。

    private String getToken() throws Exception {
        String access_token = "";
        String url = "https://login.windows.net/MyApplication_ID_here/oauth2/token";
        HttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);

        post.setHeader("Content-Type", "application/x-www-form-urlencoded");

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
        urlParameters.add(new BasicNameValuePair("client_id", "MyApplication_ID_here"));
        urlParameters.add(new BasicNameValuePair("client_secret", "MyApplication_secret_here"));
        urlParameters.add(new BasicNameValuePair("resource", "https://graph.microsoft.com"));

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        HttpResponse response = client.execute(post);
        System.out.println("Sending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + post.getEntity());
        System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

        String responseAsString = EntityUtils.toString(response.getEntity());
        System.out.println(responseAsString);
        try {
            access_token = responseAsString.split(",")[6].split("\"")[3]; // get the access_token from response 
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return access_token;
}

响应:

{"token_type":"Bearer","expires_in":"3599","ext_expires_in":"0","expires_on":"1493011626","not_before":"1493007726","resource":"https://graph.microsoft.com","access_token":"eyJ0e..."}

然后我使用 access_token 加载 memberOf 值,该值不起作用并给我 Access Token 丢失或格式错误 错误。下面是 java 代码

private void getMemberOf()
{
    HttpClient httpclient = HttpClients.createDefault();
    try
    {
        URIBuilder builder = new URIBuilder("https://graph.windows.net/MyApplication_ID_here/users/test@testABC.onmicrosoft.com/memberOf?api-version=1.6");
        URI uri = builder.build();
        HttpGet request = new HttpGet(uri);
        request.addHeader("Authorization", "Bearer " + access_token);
        request.addHeader("Content-Type", "application/json");

        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();
        System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
        if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
        }
    }
    catch (Exception e)
    {
        e.getMessage();
    }
}

响应:

Response Code : 401
{"odata.error":{"code":"Authentication_MissingOrMalformed","message":{"lang":"en","value":"Access Token missing or malformed."},"date":"2017-04-24T04:39:38","requestId":"c5aa2abe-9b37-4611-8db1-107e3ec08c14","values":null}}

谁能告诉我上面的请求哪一部分是错误的? access_token 是我设置的不对吗?

根据您的代码,您的访问令牌用于资源“https://graph.microsoft.com"(Microsoft Graph API) ,But the access token is used for "https://graph.windows.net”(AAD Graph API) :

 URIBuilder builder = new URIBuilder("https://graph.windows.net/MyApplication_ID_here/users/test@testABC.onmicrosoft.com/memberOf?api-version=1.6");

如果你想调用 Azure AD graph api ,你需要获取 Azure AD Graph API 的访问令牌。

我在通过 AD Graph API 对 Azure AD B2C 服务执行 CRUD 操作以进行用户管理时遇到了这个问题。

我的想法是获取资源的访问令牌 "graph.windows.net" 而不是我使用的是建议的我的租户 App Id URI here

* 可能会帮助遇到同样问题并来到这里的人