eBay / ebay-oauth-python-client

Python OAuth SDK: Get OAuth tokens for eBay public APIs
Other
81 stars 47 forks source link

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

Open zack-doll opened 4 years ago

zack-doll commented 4 years ago

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.

backend-stunntech commented 3 years ago

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

etowle commented 3 years ago

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