我的django项目中有自定义管道。在this example之后,我尝试获取访问令牌(从google plus获取数据),但它返回TypeError: string indices must be integers
。
自定义渠道:
def user_details(strategy, details, response, user=None, *args, **kwargs):
....
if user:
if kwargs['is_new']:
if "google" in kwargs['backend'].redirect_uri:
...
try:
social = user.social_auth.get(provider='google-plus')
access_token = logger.warning(social.extra_data['access_token'])
....
except HTTPError:
pass
.....
完整追溯:
Traceback (most recent call last):
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/apps/django_app/utils.py", line 51, in wrapper
return func(request, backend, *args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/apps/django_app/views.py", line 28, in complete
redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/actions.py", line 43, in do_complete
user = backend.complete(user=user, *args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/backends/base.py", line 41, in complete
return self.auth_complete(*args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/utils.py", line 229, in wrapper
return func(*args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/backends/google.py", line 158, in auth_complete
*args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/utils.py", line 229, in wrapper
return func(*args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/backends/oauth.py", line 398, in do_auth
return self.strategy.authenticate(*args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/strategies/django_strategy.py", line 96, in authenticate
return authenticate(*args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/django/contrib/auth/__init__.py", line 74, in authenticate
user = backend.authenticate(**credentials)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/backends/base.py", line 82, in authenticate
return self.pipeline(pipeline, *args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/backends/base.py", line 85, in pipeline
out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
File "/home/linevich/.virtualenvs/tathros/lib/python3.4/site-packages/social/backends/base.py", line 112, in run_pipeline
result = func(*args, **out) or {}
File "/home/linevich/projects/tathros/proto/pipeline.py", line 54, in user_details
auth_token = social.extra_data['access_token']
TypeError: string indices must be integers
答案 0 :(得分:1)
解决方案很简单:
auth_token = json.loads(social.extra_data)['access_token']