Java中的Microsoft Graph API个人资料图片更新

时间:2018-08-13 10:03:54

标签: office365 azure-active-directory microsoft-graph

我正在尝试使用Microsoft Graph API来更新Azure AD帐户的用户图片。

我已经遵循了 https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/profilephoto_update,对我来说似乎很清楚,但是我无法上传图片,它失败并显示RessourceNotFound错误消息。

在我的代码下面,以检索令牌并上传用户图片。检索令牌效果很好。

private String getToken() throws Exception {
    String access_token = "";
    String url = "https://login.windows.net/TENANTID/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", "APPLICATIONID"));
    urlParameters.add(new BasicNameValuePair("client_secret", "SECRET"));
    urlParameters.add(new BasicNameValuePair("resource", "https://graph.microsoft.com"));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpHost proxy = new HttpHost("PROXYADDRESS.com", 8080, "http");
    RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
    post.setConfig(config);

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

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

现在我尝试更新一个帐户的用户图片。

public String updatePicture(String token) throws ClientProtocolException, IOException, URISyntaxException {
    File file = new File("C:\\USERPICTURE.jpg");
    byte[] bytesArray = new byte[(int) file.length()];
    FileInputStream fis = new FileInputStream(file);
    fis.read(bytesArray);
    fis.close();
    byte[] bytesEncoded = new Base64().encode(bytesArray);

    String url = "https://graph.microsoft.com/v1.0/users/USERPRINCIPALNAMEOFTHEUSER@DOMAINMAIL.COM/photo/$value";
    HttpClient client = HttpClients.createDefault();

    HttpPut request = new HttpPut(url);
    request.setHeader("Authorization", "Bearer " + token);
    request.setHeader("Content-Type", "image/jpeg");
    request.setEntity(new ByteArrayEntity(bytesEncoded));

    HttpHost proxy = new HttpHost("PROXYADDRESS.COM", 8080, "http");
    RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
    request.setConfig(config);

    HttpResponse response = client.execute(request);
    logger.debug("Sending 'PUT' request to URL : " + url);
    logger.debug("Post parameters : " + request.getEntity());
    logger.debug("Response Code : " + response.getStatusLine().getStatusCode());

    String responseAsString = EntityUtils.toString(response.getEntity());
    logger.debug(responseAsString);
    return responseAsString;
}

这就是我的结果。

2018-08-13 11:54:10,511 DEBUG (main) [IDM(getToken:391)] Sending 'POST' request to URL : https://login.windows.net/XXXXXXX/oauth2/token
2018-08-13 11:54:10,511 DEBUG (main) [IDM(getToken:392)] Post parameters : [Content-Type: application/x-www-form-urlencoded,Content-Length: 182,Chunked: false]
2018-08-13 11:54:10,511 DEBUG (main) [IDM(getToken:393)] Response Code : 200
2018-08-13 11:54:10,511 DEBUG (main) [IDM(getToken:398)] XXXXXXXXXXXX
2018-08-13 11:54:11,791 DEBUG (main) [IDM(updatePicture:448)] Sending 'PATCH' request to URL : https://graph.microsoft.com/v1.0/users/USERPRINCIPALNAME@XXXX.COM/photo/$value
2018-08-13 11:54:11,791 DEBUG (main) [IDM(updatePicture:449)] Post parameters : [Content-Length: 5252,Chunked: false]
2018-08-13 11:54:11,791 DEBUG (main) [IDM(updatePicture:450)] Response Code : 404
2018-08-13 11:54:11,791 DEBUG (main) [IDM(updatePicture:453)] {
  "error": {
    "code": "ResourceNotFound",
    "message": "Resource could not be discovered.",
    "innerError": {
      "request-id": "XXXXXXXXXXXXXXXXX",
      "date": "2018-08-13T09:54:11"
    }
  }
}

有人有解决的办法吗? 谢谢

2 个答案:

答案 0 :(得分:1)

因此,除base64转换外,我的代码中没有错误。由于用户没有邮箱,因此失败。如果我选择一个有邮箱的用户,它将返回SUCCESS 200并更新图片。

现在,最后一个问题,您知道如何删除图片吗?在Microsoft Graph文档中找不到

谢谢

答案 1 :(得分:1)

  

您知道如何删除图片吗?在Microsoft Graph文档中找不到

据我所知,没有删除照片架API。在photo Rest API中,我们发现没有删除照片API。

我尝试用邮递员删除照片。我知道该方法不允许

enter image description here

我还通过office365网站检查了我的个人资料,并且没有删除照片的选项。但是,如果您想更改照片,可以将其更新为您提到的另一张照片。

enter image description here