当我尝试获取 access_token 时,Python-social-auth 管道返回 TypeError

标签 python django google-plus python-social-auth

我的 django 项目中有自定义管道。正在关注this example我正在尝试获取访问 token (以从 google plus 获取数据),但它返回给我 TypeError: stringindexs 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

最佳答案

解决方案很简单:

auth_token = json.loads(social.extra_data)['access_token']

关于当我尝试获取 access_token 时,Python-social-auth 管道返回 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36751079/

相关文章:

python - 值更新的 Json 写入问题

python - 根据值从多列之一进行 Pandas 查找

python - 将字符串分成单独的列 pandas python

html - Bootstrap Jumbotron CSS 模板不适用于 Django 元素

Django 休息框架缓存策略

python - 如何在生产服务器上禁用 NoReverseMatch 错误电子邮件?

java - 在 Android 应用程序上从 Google Plus 帐户检索电子邮件

php - 在服务器端从 Android 验证用户

google-plus - Google+ 登录按钮自定义图片

远程服务器上的 Python 进程需要通过 ssh 连接到其他服务器并继续运行