Check that attachment actually exists. Also, the $attachment might not contain the "contentUrl" property: you should send a GET request to the attachment resource to actually retrieve the full body:
  $attachment = $mirror_service->timeline->attachments->get($timeline_item_id, $attachment.getId());