在 Microsoft Graph API 中使用 MSAL 刷新令牌的正确实现是什么?
What is the correct implementation to refresh a token with MSAL in Microsoft Graph API?
我有一个桌面程序需要用户权限才能访问 Sharepoint 列表。我已经实现了必要的结构来验证用户并使用 acquire_token_silent() 方法检索其令牌。由于上传到列表可能需要一些时间,我想确保令牌已刷新。我无法让用户再次登录,因为这会由于重定向到登录表单而停止上传过程。在做了一些研究之后,我实现了下一个代码:
在上传数据刷新令牌并检查之前:
token = _get_token_from_cache(MYSCOPE)
if not token:
return redirect(login page)
数据上传期间:
token = _get_token_from_cache(MYSCOPE)
if not token:
token = methodB(MYSCOPE)
if not token:
return redirect(login page)
函数 methodB 是:
def methodB(scope):
cache = _load_cache()
a = _build_msal_app(cache=cache)
accounts = cca.get_accounts()
if accounts: # So all account(s) belong to the current signed-in user
result = a.acquire_token_silent(scope, account=accounts[0])
if not result:
result = app.acquire_token_by_xxx(scope)
_save_cache(cache)
return result
和开始时一样,我已经检查了用户是否登录我想知道是否有任何方法可以避免在上传过程中登录重定向。提前致谢。
如果我们已经有最终用户之前用于登录的一些帐户,acquire_token_silent
会在缓存中为该帐户找到一个令牌,它会自动为您处理令牌刷新。
但是如果缓存中没有合适的token,则需要向AAD发送请求获取token。
您可以使用 Username Password Flow to avoid the login redirect, only this one fits your situation although it is not recommended. There is a sample 和 Python。
accounts = app.get_accounts(username=config["username"])
if accounts:
logging.info("Account(s) exists in cache, probably with token too. Let's try.")
result = app.acquire_token_silent(config["scope"], account=accounts[0])
if not result:
logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
result = app.acquire_token_by_username_password(
config["username"], config["password"], scopes=config["scope"])
配置文件如下所示:
{
"authority": "https://login.microsoftonline.com/<your tenant id>",
"client_id": "your_client_id",
"username": "your_username@your_tenant.com",
"password": "This is a sample only. You better NOT persist your password.",
"scope": ["User.ReadBasic.All"],
"endpoint": "https://graph.microsoft.com/v1.0/users"
}
我知道这个问题已经有答案了。我想提供一些更相关的信息供您考虑。
OP 一开始就提到有问题的应用程序是“桌面程序”,但代码片段源自 MSAL Python 的网络应用程序示例。对于桌面程序,您最好从 MSAL's interactive sample 开始,它还负责刷新您的令牌。
这样就不需要使用用户名密码的方法了,反正不推荐。
我有一个桌面程序需要用户权限才能访问 Sharepoint 列表。我已经实现了必要的结构来验证用户并使用 acquire_token_silent() 方法检索其令牌。由于上传到列表可能需要一些时间,我想确保令牌已刷新。我无法让用户再次登录,因为这会由于重定向到登录表单而停止上传过程。在做了一些研究之后,我实现了下一个代码:
在上传数据刷新令牌并检查之前:
token = _get_token_from_cache(MYSCOPE)
if not token:
return redirect(login page)
数据上传期间:
token = _get_token_from_cache(MYSCOPE)
if not token:
token = methodB(MYSCOPE)
if not token:
return redirect(login page)
函数 methodB 是:
def methodB(scope):
cache = _load_cache()
a = _build_msal_app(cache=cache)
accounts = cca.get_accounts()
if accounts: # So all account(s) belong to the current signed-in user
result = a.acquire_token_silent(scope, account=accounts[0])
if not result:
result = app.acquire_token_by_xxx(scope)
_save_cache(cache)
return result
和开始时一样,我已经检查了用户是否登录我想知道是否有任何方法可以避免在上传过程中登录重定向。提前致谢。
如果我们已经有最终用户之前用于登录的一些帐户,acquire_token_silent
会在缓存中为该帐户找到一个令牌,它会自动为您处理令牌刷新。
但是如果缓存中没有合适的token,则需要向AAD发送请求获取token。
您可以使用 Username Password Flow to avoid the login redirect, only this one fits your situation although it is not recommended. There is a sample 和 Python。
accounts = app.get_accounts(username=config["username"])
if accounts:
logging.info("Account(s) exists in cache, probably with token too. Let's try.")
result = app.acquire_token_silent(config["scope"], account=accounts[0])
if not result:
logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
result = app.acquire_token_by_username_password(
config["username"], config["password"], scopes=config["scope"])
配置文件如下所示:
{
"authority": "https://login.microsoftonline.com/<your tenant id>",
"client_id": "your_client_id",
"username": "your_username@your_tenant.com",
"password": "This is a sample only. You better NOT persist your password.",
"scope": ["User.ReadBasic.All"],
"endpoint": "https://graph.microsoft.com/v1.0/users"
}
我知道这个问题已经有答案了。我想提供一些更相关的信息供您考虑。
OP 一开始就提到有问题的应用程序是“桌面程序”,但代码片段源自 MSAL Python 的网络应用程序示例。对于桌面程序,您最好从 MSAL's interactive sample 开始,它还负责刷新您的令牌。
这样就不需要使用用户名密码的方法了,反正不推荐。