Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lcoustillac opened this issue Nov 6, 2023 · 1 comment

Comments

@lcoustillac
Copy link

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).

lcoustillac added a commit to lcoustillac/wikitools3 that referenced this issue Nov 6, 2023
lcoustillac added a commit to lcoustillac/wikitools3 that referenced this issue Nov 6, 2023
@lcoustillac
Copy link
Author

Note that base64.encodestring apparently became base64.encodebytes in python 3.9 so I think it should be base64.encodebytes(f"{wiki.auth}:{wiki.httppass}".encode()).decode()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant