【问题标题】:Microsoft Graph returns 400 when querying default photo查询默认照片时 Microsoft Graph 返回 400
【发布时间】:2024-05-04 14:50:02
【问题描述】:

我正在查询用户的照片元数据,然后使用元数据中的 ID 来查询实际照片:

https://graph.microsoft.com/v1.0/users/<user id>/photo 返回

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('<user id>')/photo/$entity",
    "@odata.mediaContentType": "image/jpeg",
    "@odata.mediaEtag": "W/\"...\"",
    "id": "default",
    "height": 64,
    "width": 64
}

请注意,它的 ID 是 default。然后,当我尝试使用 https://graph.microsoft.com/v1.0/users/&lt;user id&gt;/photos/default/$value 获取内容时,图形返回 HTTP 400 和

{
    "error": {
        "code": "ErrorInvalidImageId",
        "message": "The image ID is not valid",
        "innerError": {
            "date": "2021-12-02T14:59:49",
            "request-id": "...",
            "client-request-id": "..."
        }
    }
}

如果我将上述 URL 中的 default 替换为 64x64(元数据中的照片尺寸),一切都很好,即 https://graph.microsoft.com/v1.0/users/&lt;user id&gt;/photos/64x64/$value 返回照片。

对于许多用户来说,从https://graph.microsoft.com/v1.0/users/&lt;user id&gt;/photo 返回的 ID 看起来像 64x64,而且这种 ID 总是有效的。

为什么 Graph 会为某些用户返回 default,我应该如何处理?每当我收到default 时,从宽度和高度创建 ID 是否安全?

【问题讨论】:

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


    【解决方案1】:

    我们可以参考official document来查找出现这种情况的原因。

    如果我们调用url/users/&lt;user id&gt;/photo来获取照片的元数据,我们会得到profilePhoto作为response object,其中包含id, height, width,id由高度和宽度组成。

    那我们看看这个api的this feature,它是专门用来查询特定照片尺寸的元数据的,所以如果id代表照片尺寸,我们可以用id作为查询参数,但是如果id是其他一些值,在这里不起作用。

    我的想法关于您的情况是 该 api 是为使用照片大小作为查询参数而设计的,我们可能始终使用高度和宽度作为查询参数 这样我们就不需要检查它是否返回正确的 id 进行查询。

    【讨论】: