question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting the decoder parameter in the get_auth_session function to json.loads when authenticating with google oauth2 returns an error

See original GitHub issue

I have the following code token = request.GET.get(‘code’) data = { ‘code’: token, ‘redirect_uri’: ANGULAR_LOGIN_URL, ‘grant_type’: ‘authorization_code’} session = auth_manager.get_auth_session(data=data, decoder=json.loads)

which gives me the following error,

TypeError at /login/google/
can't use a string pattern on a bytes-like object
Request Method: GET
Request URL:    http://127.0.0.1:8000/login/google/?code=4/dX8nbHWAW7WTMWr4zd6aigqygpvD.gn_o-kuFOPUcmmS0T3UFEsOglLpHhgI
Django Version: 1.6
Exception Type: TypeError
Exception Value:    
can't use a string pattern on a bytes-like object
Exception Location: /usr/lib64/python3.3/json/decoder.py in decode, line 352

and the traceback

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/login/google/?code=4/dX8nbHWAW7WTMWr4zd6aigqygpvD.gn_o-kuFOPUcmmS0T3UFEsOglLpHhgI

Django Version: 1.6 Python Version: 3.3.2 Installed Applications: (‘django.contrib.admin’, ‘django.contrib.auth’, ‘django.contrib.contenttypes’, ‘django.contrib.sessions’, ‘django.contrib.messages’, ‘django.contrib.staticfiles’, ‘corsheaders’, ‘restless’, ‘blog’, ‘jobs’, ‘meetups’, ‘tweets’, ‘users’) Installed Middleware: (‘django.contrib.sessions.middleware.SessionMiddleware’, ‘django.middleware.common.CommonMiddleware’, ‘django.middleware.csrf.CsrfViewMiddleware’, ‘django.contrib.auth.middleware.AuthenticationMiddleware’, ‘django.contrib.messages.middleware.MessageMiddleware’, ‘django.middleware.clickjacking.XFrameOptionsMiddleware’, ‘corsheaders.middleware.CorsMiddleware’)

Traceback: File “/home/lucas/Programming/Python/Django/BoulderDjangoDev/venv/lib/python3.3/site-packages/django/core/handlers/base.py” in get_response

  1.                 response = wrapped_callback(request, _callback_args, *_callback_kwargs)
    
    File “/home/lucas/Programming/Python/Django/BoulderDjangoDev/venv/lib/python3.3/site-packages/django/views/generic/base.py” in view
  2.         return self.dispatch(request, _args, *_kwargs)
    
    File “/home/lucas/Programming/Python/Django/BoulderDjangoDev/venv/lib/python3.3/site-packages/django/views/generic/base.py” in dispatch
  3.     return handler(request, _args, *_kwargs)
    
    File “/home/lucas/Programming/Python/Django/BoulderDjangoDev/BoulderDjangoDev/oauth/views.py” in get
  4.         request=request, account_type=GOOGLE, auth_manager=google_auth)
    
    File “/home/lucas/Programming/Python/Django/BoulderDjangoDev/BoulderDjangoDev/oauth/views.py” in OAuth2Callback
  5.     session = auth_manager.get_auth_session(data=data, decoder=json.loads)
    
    File “/home/lucas/Programming/Python/Django/BoulderDjangoDev/venv/lib/python3.3/site-packages/rauth/service.py” in get_auth_session
  6.     return self.get_session(self.get_access_token(method, **kwargs))
    
    File “/home/lucas/Programming/Python/Django/BoulderDjangoDev/venv/lib/python3.3/site-packages/rauth/service.py” in get_access_token
  7.     access_token, = process_token_request(r, decoder, key)
    
    File “/home/lucas/Programming/Python/Django/BoulderDjangoDev/venv/lib/python3.3/site-packages/rauth/service.py” in process_token_request
  8.     data = decoder(r.content)
    
    File “/usr/lib64/python3.3/json/init.py” in loads
  9.     return _default_decoder.decode(s)
    
    File “/usr/lib64/python3.3/json/decoder.py” in decode
  10.     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    

Exception Type: TypeError at /login/google/ Exception Value: can’t use a string pattern on a bytes-like object

I’m not too sure why I am getting this error. I looked at

https://github.com/litl/rauth/issues/110

but that has not helped

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
maxcountrymancommented, Dec 25, 2013
session = auth_manager.get_auth_session(data=data, decoder=lambda b: json.loads(str(b)))
0reactions
maxcountrymancommented, Mar 12, 2014

You can also pass in a custom decoder now. Closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

rauth: the JSON object must be str, not 'bytes' - Stack Overflow
If i remove the decoder=json.loads , I get the following error: Decoder failed to handle access_token with data as returned by provider. A ......
Read more >
Using OAuth 2.0 for Server to Server Applications | Authorization
Preparing to make an authorized API call; Calling Google APIs; JWT error codes; Addendum: Service account authorization without OAuth.
Read more >
Python JSON Parsing using json.load() and loads() - PYnative
loads () method, you can turn JSON encoded/formatted data into Python Types this process is known as JSON decoding. Python built-in module json...
Read more >
google.oauth2.service_account module
oauth2.service_account module¶. Service Accounts: JSON Web Token (JWT) Profile for OAuth 2.0. This module implements the JWT Profile for OAuth 2.0 Authorization ...
Read more >
Python Tutorial: json.dump(s) & json.load(s) - 2021
dumps()" returns a string as indicated by the "s" at the end of "dumps". This process is called encoding. Let's write it to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found