ebay-oauth-python-client icon indicating copy to clipboard operation
ebay-oauth-python-client copied to clipboard

get_application_token -- TypeError: a bytes-like object is required, not 'str'

Open zack-doll opened this issue 5 years ago • 2 comments
trafficstars

Trying to get an application token and am getting Traceback (most recent call last): File "query.py", line 150, in <module> main() File "query.py", line 142, in main app_token = oauth.get_application_token(env_type = environment.PRODUCTION, scopes = constant.SCOPE) File "/mnt/c/Users/Zack/Documents/src/CardMonitor/ebay_oauth_python_client/oauthclient/oauth2api.py", line 75, in get_application_token headers = model.util._generate_request_headers(credential) File "/mnt/c/Users/Zack/Documents/src/CardMonitor/ebay_oauth_python_client/oauthclient/model/util.py", line 23, in _generate_request_headers b64_encoded_credential = base64.b64encode(credential.client_id + ':' + credential.client_secret) File "/usr/lib/python3.8/base64.py", line 58, in b64encode encoded = binascii.b2a_base64(s, newline=False) TypeError: a bytes-like object is required, not 'str'

There don't appear to be any issues loading credentials. My config file is an exact copy of the provided sample, with my keys replaced of course.

And this is what my script looks like thus far: def main():\ print("Hello World!") print("loading credential...") credentialutil.load('config.json') print("...loaded?") oauth = oauth2api() app_token = oauth.get_application_token(env_type = environment.PRODUCTION, scopes = constant.SCOPE) print(app_token)

Thanks for any help! This is my first time using OAuth and also my first GitHub issue... Cheers

P.S. My apologies, I'm unsure how to include linebreaks here.

zack-doll avatar Sep 20 '20 02:09 zack-doll

@dollZack I got the same issue. Is there any solution?

backend-stunntech avatar Feb 08 '21 10:02 backend-stunntech

Python 3 introduced some changes to text and binary data types (see Porting Python 2 Code to Python 3). I got around this issue with the following changes to oauthclient/model/util.py:

diff --git a/oauthclient/model/util.py b/oauthclient/model/util.py
index 4340361..9475e7d 100644
--- a/oauthclient/model/util.py
+++ b/oauthclient/model/util.py
@@ -20,10 +20,13 @@ import base64
 
 def _generate_request_headers(credential):
 
-    b64_encoded_credential = base64.b64encode(credential.client_id + ':' + credential.client_secret)
+    credentials = credential.client_id + ':' + credential.client_secret
+    credentials_bytes = credentials.encode('ascii')
+    b64_credentials_bytes = base64.b64encode(credentials_bytes)
+    b64_credentials = b64_credentials_bytes.decode('ascii')
     headers = {
             'Content-Type': 'application/x-www-form-urlencoded',
-            'Authorization': 'Basic ' + b64_encoded_credential
+            'Authorization': 'Basic ' + b64_credentials
     }
 
     return headers

This change is part of #6 (unmerged).

etowle avatar Mar 07 '21 21:03 etowle