Skip to content

wiki.Wiki fails when setting httpuser and httppass as api.py passes a string to base64.encodestring #9

@lcoustillac

Description

@lcoustillac

Hi, I am still trying to migrate a python 2.7 script to python3, using wikitools3, as in issue #8.

Now am I using the develop branch of this project, not a release. My script fails again, for a different reason.

So if I simplify it again, it boils down to this new test_wikitools.py in which I added a login and password to wiki.Wiki:

#!/usr/bin/env python
""" quick and dirty script aimed at debugging wikitools3 """
""" see issues https://github.com/mediawiki-client-tools/wikitools3/issues """

import os
from wikitools3 import wiki

# now I am using my company's wiki api with credentials
api_url = os.environ.get('WIKI_URL')
username = os.environ.get('WIKI_USER')
password = os.environ.get('WIKI_PASSWORD')
print("api_url: " + str(api_url) + " username: " + username)
if not password:
    print("Empty variable: WIKI_PASSWORD")
    sys.exit(1)
site = wiki.Wiki(api_url, username, password, True)

After making sure I set the WIKI_URL, WIKI_USER and WIKI_PASSWORD variables and run this script, it fails like this:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/base64.py", line 510, in _input_type_check
    m = memoryview(s)
TypeError: memoryview: a bytes-like object is required, not 'str'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./test_wikitools.py", line 16, in <module>
    site = wiki.Wiki(api_url, username, password, True)
  File "/workdir/wikitools3/wiki.py", line 112, in __init__
    self.setSiteinfo()
  File "/workdir/wikitools3/wiki.py", line 130, in setSiteinfo
    req = api.APIRequest(self, params)
  File "/workdir/wikitools3/api.py", line 99, in __init__
    base64.encodestring(f"{wiki.auth}:{wiki.httppass}")
  File "/usr/local/lib/python3.8/base64.py", line 540, in encodestring
    return encodebytes(s)
  File "/usr/local/lib/python3.8/base64.py", line 527, in encodebytes
    _input_type_check(s)
  File "/usr/local/lib/python3.8/base64.py", line 513, in _input_type_check
    raise TypeError(msg) from err
TypeError: expected bytes-like object, not str

Apparently base64.encodestring expects bytes not a string. This patch on api.py line 99 seems to fix the problem:

         if wiki.auth:
             self.headers["Authorization"] = "Basic {0}".format(
-                base64.encodestring(f"{wiki.auth}:{wiki.httppass}")
+                base64.encodestring(f"{wiki.auth}:{wiki.httppass}".encode()).decode()
             ).replace("\n", "")

(And then my main script will fail later for a different reason, but I guess this will be a seperate issue).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions