title: Accessing Shared Mailbox Using Exchangelib — Python
body: <p>Trying to Access a Shared Folder using the following code :</p>
  <pre><code>credentials = Credentials(username = user_name, password = &quot;secret&quot;)
  config = Configuration(server ='outlook.office365.com', credentials = credentials,  auth_type=NTLM)
  account = Account(primary_smtp_address = 'shared_mail@domain.com', credentials = credentials, autodiscover = False, config = config, access_type = DELEGATE,)
  </code></pre>
  <p>The above three lines of Code work perfectly but we are unable to get the <code>root</code>,
  the following code : <code>account.root.tree() or account.root</code> throws the following error:</p>
  <pre><code>KeyError                                  Traceback (most recent call last)
      ~\anaconda3\lib\site-packages\cached_property.py in __get__(self, obj, cls)
           68                 # check if the value was computed before the lock was acquired
      ---&gt; 69                 return obj_dict[name]
           70 
      KeyError: 'root'
      During handling of the above exception, another exception occurred:
      ErrorNonExistentMailbox                   Traceback (most recent call last)
      &lt;ipython-input-46-a90a4f76ca21&gt; in &lt;module&gt;
            2 logging.basicConfig(level=logging.DEBUG)
            3 
      ----&gt; 4 account.root.tree()
      ~\anaconda3\lib\site-packages\cached_property.py in __get__(self, obj, cls)
           71             except KeyError:
           72                 # if not, do the calculation and release the lock
      ---&gt; 73                 return obj_dict.setdefault(name, self.func(obj))
           74 
           75 
      ~\anaconda3\lib\site-packages\exchangelib\account.py in root(self)
          268     @threaded_cached_property
          269     def root(self):
      --&gt; 270         return Root.get_distinguished(account=self)
          271 
          272     @threaded_cached_property
      ~\anaconda3\lib\site-packages\exchangelib\folders\roots.py in get_distinguished(cls, account)
          107             return cls.resolve(
          108                 account=account,
      --&gt; 109                 folder=cls(account=account, name=cls.DISTINGUISHED_FOLDER_ID, is_distinguished=True)
          110             )
          111         except ErrorFolderNotFound:
      ~\anaconda3\lib\site-packages\exchangelib\folders\base.py in resolve(cls, account, folder)
          485     def resolve(cls, account, folder):
          486         # Resolve a single folder
      --&gt; 487         folders = list(FolderCollection(account=account, folders=[folder]).resolve())
          488         if not folders:
          489             raise ErrorFolderNotFound('Could not find folder %r' % folder)
      ~\anaconda3\lib\site-packages\exchangelib\folders\collections.py in resolve(self)
          254         additional_fields = self.get_folder_fields(target_cls=self._get_target_cls(), is_complex=None)
          255         for f in self.__class__(account=self.account, folders=resolveable_folders).get_folders(
      --&gt; 256                 additional_fields=additional_fields
          257         ):
          258             yield f
      ~\anaconda3\lib\site-packages\exchangelib\folders\collections.py in get_folders(self, additional_fields)
          317                 folders=self.folders,
          318                 additional_fields=additional_fields,
      --&gt; 319                 shape=ID_ONLY,
          320         ):
          321             yield f
      ~\anaconda3\lib\site-packages\exchangelib\services\get_folder.py in call(self, folders, additional_fields, shape)
           32             **dict(
           33                 additional_fields=additional_fields,
      ---&gt; 34                 shape=shape,
           35             )
           36         )):
      ~\anaconda3\lib\site-packages\exchangelib\services\common.py in _pool_requests(self, payload_func, items, **kwargs)
          538         for i, chunk in enumerate(chunkify(items, self.chunk_size), start=1):
          539             log.debug('Processing %s chunk %s containing %s items', self.__class__.__name__, i, len(chunk))
      --&gt; 540             for elem in self._get_elements(payload=payload_func(chunk, **kwargs)):
          541                 yield elem
          542 
      ~\anaconda3\lib\site-packages\exchangelib\services\common.py in _get_elements_in_response(self, response)
          401     def _get_elements_in_response(self, response):
          402         for msg in response:
      --&gt; 403             container_or_exc = self._get_element_container(message=msg, name=self.element_container_name)
          404             if isinstance(container_or_exc, (bool, Exception)):
          405                 yield container_or_exc
      ~\anaconda3\lib\site-packages\exchangelib\services\common.py in _get_element_container(self, message, response_message, name)
          360         # rspclass == 'Error', or 'Success' and not 'NoError'
          361         try:
      --&gt; 362             raise self._get_exception(code=response_code, text=msg_text, msg_xml=msg_xml)
          363         except self.ERRORS_TO_CATCH_IN_RESPONSE as e:
          364             return e
      ErrorNonExistentMailbox: Mailbox does not exist.
  </code></pre>
  <p>The same code seems to be working here : <a href="https://medium.com/@theamazingexposure/accessing-shared-mailbox-using-exchangelib-python-f020e71a96ab" rel="nofollow noreferrer">https://medium.com/@theamazingexposure/accessing-shared-mailbox-using-exchangelib-python-f020e71a96ab</a></p>
  <p>Also checked this thread <a href="https://github.com/ecederstrand/exchangelib/issues/391" rel="nofollow noreferrer">https://github.com/ecederstrand/exchangelib/issues/391</a> and tried almost all the solutions but facing the same error.</p>