0

I'm trying to access an API which has requires authentication. However I keep getting the error which reads " a bytes-like object is required, not 'str' " for the base64encode line.

user = input("user: example")
password = getpass.getpass("password: example1234")
authCred = base64.b64encode(user + ":" + password)

I tried to change the code to:

user = input("user: example")
password = getpass.getpass("password: example1234")
authCred = base64.b64encode(user.encode('ascii') + ":" + password.encode('ascii')).decode('ascii')

But then I get the error "can't concat str to bytes"

Does anyone know how I can fix this?

1
  • were you able to fix this? Commented Nov 26, 2019 at 5:44

2 Answers 2

1

Your attempted fix didn't work because you tried to mix encoded and non-encoded strings.

Make a string that is the username plus a colon plus the password, then encode that whole string:

userpass = user + ":" + pass
authCred = base64.b64encode(userpass.encode('ascii'))
Sign up to request clarification or add additional context in comments.

1 Comment

Alternately, use b':' for the colon when assembling the results afterwards.
0

does encoding it as bytes help?

authcred = bytes(authcred , encoding= 'utf-8')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.