Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Last active October 31, 2024 11:38
Show Gist options
  • Save chgeuer/d7079a86dd36d2e78b99db906bb14b17 to your computer and use it in GitHub Desktop.
Save chgeuer/d7079a86dd36d2e78b99db906bb14b17 to your computer and use it in GitHub Desktop.

Grab a user image from Microsoft Graph

Mix.install([
  {:req, "~> 0.5.7"},
  {:kino, "~> 0.14.2"}
])

Grab a user image from Microsoft Graph

name = "chgeuer@microsoft.com"
  
get_access_token_cmd = "az account get-access-token --resource-type ms-graph"

{json_response, 0} =
  System.cmd("cmd.exe", ["/c #{get_access_token_cmd}"])

access_token = :json.decode(json_response)["accessToken"]

response =
  Req.new(
    method: :get,
    url: "https://graph.microsoft.com/v1.0/users/{name}/photo/$value",
    path_params_style: :curly, 
    path_params: [name: name],
    auth: {:bearer, access_token}
  )
  |> Req.request!()
  |> case do
    %Req.Response{status: 200, headers: %{"content-type" => [_mime_type]}, body: image_bytes} ->
      image_bytes

    %Req.Response{status: 404, body: %{"error" => %{"code" => code}}}
      when code in ["ErrorEntityNotFound", "ImageNotFound"] ->
      {:error, :not_found}
  end
@chgeuer
Copy link
Author

chgeuer commented Oct 31, 2024

2024-10-30--17-15-59

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment