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

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

Open
dollZack opened this issue Sep 20, 2020 · 2 comments

Comments

@dollZack
Copy link

dollZack commented Sep 20, 2020

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
Copy link

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

@etowle
Copy link

etowle commented Mar 7, 2021

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

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

No branches or pull requests

3 participants