如何使用gthon与python-social-auth

时间:2017-07-02 02:20:53

标签: python-social-auth gspread

我正在尝试使用gspread和python-social-auth,我按照文档中的示例描述创建了这个类以用作凭据存储:

class Credentials(object):
    def __init__ (self, access_token=None, scope=None):
        self.access_token = access_token
        self.scope=scope

    def refresh (self, http):
        # get new access_token
        # this only gets called if access_token is None
        pass

在我的代码中我使用过:

import gspread

credentials = Credentials(access_token=user.social_auth.get_access_token(),
              scope=['https://spreadsheets.google.com/feeds'])
sh = gspread.authorize(credentials)

使用以下方式询问API的任何回复:

sh.get_spreadsheets_feed()

控制台上显示此错误:

*** RequestError: (401, '401: <HTML>\n<HEAD>\n<TITLE>Token invalid - AuthSub token has wrong scope</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Token invalid - AuthSub token has wrong scope</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n')

我已根据我的设置定义了范围,这样做效果很好,例如尝试从Google通讯录中获取联系人

SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
     'https://www.googleapis.com/auth/contacts.readonly',
     'https://www.googleapis.com/auth/spreadsheets.readonly' 
]

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您还必须将这些范围添加到Python Social Auth设置:

SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
    ...
    'https://www.googleapis.com/auth/spreadsheets.readonly',
    'https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive.file'
]
相关问题