首页 > 解决方案 > Google OAuth2 通过访问令牌获取刷新令牌

问题描述

我有这样的python授权:

def return_token():
    return get_oauth2_token()


def disable_stout():
    o_stdout = sys.stdout
    o_file = open(os.devnull, 'w')
    sys.stdout = o_file
    return (o_stdout, o_file)


def enable_stout(o_stdout, o_file):
    o_file.close()
    sys.stdout = o_stdout


def get_oauth2_token():
    CLIENT_ID = '123'
    CLIENT_SECRET = '123'
    SCOPE = \
        'https://www.googleapis.com/auth/plus.login ' + \
        'https://www.googleapis.com/auth/plus.me ' + \
        'https://www.googleapis.com/auth/userinfo.email ' + \
        'https://www.googleapis.com/auth/userinfo.profile ' + \
        'https://mail.google.com/ ' + \
        'https://www.googleapis.com/auth/gmail.readonly ' + \
        'https://www.googleapis.com/auth/gmail.settings.basic'

    REDIRECT_URI = 'http://localhost:8080'

    o_stdout, o_file = disable_stout()

    flow = OAuth2WebServerFlow(
        client_id=CLIENT_ID,
        client_secret=CLIENT_SECRET,
        scope=SCOPE,
        redirect_uri=REDIRECT_URI)

    storage = Storage('creds.data')
    credentials = run_flow(flow, storage)
    enable_stout(o_stdout, o_file)

    print("access_token: %s" % credentials.access_token)
    print("refresh_token: %s" % credentials.refresh_token)


if __name__ == '__main__':
    return_token()

此代码工作和授权返回访问刷新令牌。只有当我拥有访问刷新令牌时,我在后端的所有逻辑才会起作用。前端使用react-native-google-signin库的开发人员和此库不返回刷新令牌。我有一个问题:我能refresh tokenaccess token吗?

标签: pythongoogle-authentication

解决方案


推荐阅读