Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

URL string in non ascii code causes exception

Open harada-masanori opened this issue 1 year ago • 0 comments

Thank you for your contributions.

I attempted to use this example with a URL that contains Japanese characters: https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/onedrive/files/get_by_abs_url.py

However, it resulted in the following exception:

Traceback (most recent call last):
  File "c:\a\auth_test1.py", line 68, in <module>
    file_item = client.shares.by_url(file_abs_url).drive_item.get().execute_query()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\runtime\client_object.py", line 52, in execute_query
    self.context.execute_query()
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\runtime\client_runtime_context.py", line 185, in execute_query
    self.pending_request().execute_query(qry)
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\runtime\client_request.py", line 36, in execute_query
    request = self.build_request(query)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\runtime\odata\request.py", line 36, in build_request
    request = RequestOptions(query.url)
                             ^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\runtime\queries\read_entity.py", line 35, in url
    return self.binding_type.resource_url
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\runtime\client_object.py", line 214, in resource_url
    return self.context.service_root_url() + str(self.resource_path)
                                             ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\runtime\paths\resource_path.py", line 28, in __str__
    return self.to_url()
           ^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\runtime\paths\resource_path.py", line 35, in to_url
    segments.insert(0, path.segment)
                       ^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\onedrive\internal\paths\shared.py", line 23, in segment
    return _url_to_shared_token(self._key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\office365\onedrive\internal\paths\shared.py", line 11, in _url_to_shared_token
    value = base64.b64encode(url.encode("ascii")).decode("ascii")
                             ^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode characters in position 64-74: ordinal not in range(128)

I found that changing the encoding from 'ascii' to 'utf-8' resolved the issue for me, but I'm unsure of any potential side effects. Here's the modified line of code:

value = base64.b64encode(url.encode("utf-8")).decode("ascii")

Is it possible to implement this change, or are there any workarounds available?

Thank you in advance for your assistance!

harada-masanori avatar Jun 28 '24 06:06 harada-masanori