如何在firebase DB Auth令牌到期之前刷新它?

时间:2017-09-23 02:46:20

标签: python firebase-realtime-database firebase-authentication access-token

我正在使用python写入我的firebase数据库。我有数以千计的数据集需要数小时才能完成。我的程序一再被此错误停止

  
    

“身份验证令已过期”

  

我搜索了解决方案但找不到任何解决方案,我后来发现可以检查到期前剩余的时间并刷新令牌,以便数据传输不会中断。 如何在令牌过期之前刷新令牌?

2 个答案:

答案 0 :(得分:1)

我正在将django与firebase和pyrebase模块一起使用。我也遇到过类似的问题。我在github上找到了解决方案:-> https://github.com/thisbejim/Pyrebase/issues/244 在此代码之后->

config = {
    'apiKey': "your-api-key",
    'authDomain': "your-auth-domain",
    'databaseURL': "your-db-url",
    'projectId': "your-project-id",
    'storageBucket': "your-storage-bucket",
    'messagingSenderId': "your-messaging-sender-id",
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
user = auth.sign_in_with_email_and_password("email@sthn.com", "strong-password")

添加此

# A user's idToken expires after 1 hour, so be sure to use the user's refreshToken to avoid stale tokens.
user = auth.refresh(user['refreshToken'])
# now we have a fresh token
user['idToken']

答案 1 :(得分:0)

您可以使用google oauth2 lib在令牌即将到期时刷新令牌,如下所示:

import httplib2
from oauth2client.service_account import ServiceAccountCredentials
credentials = ServiceAccountCredentials.from_json_keyfile_name("service-account.json", "{scope}")
print(credentials.get_access_token())
credentials.refresh(httplib2.Http())
print(credentials.get_access_token())