配置Hadoop-Azure ABFS驱动以Azure CLI用户身份认证存储账户
配置ABFS使用RefreshTokenBasedTokenProvider实现终端用户身份认证
一、核心配置项的获取方法
1. fs.azure.account.oauth2.client.id
这个值固定为Azure CLI的内置应用ID:04b07795-8ddb-461a-bbee-02f9e1bf7b46。因为你是通过Azure CLI登录的用户,ABFS要复用这个身份,必须使用Azure CLI在Azure AD中注册的应用ID。
2. fs.azure.account.oauth2.refresh.endpoint
默认的https://login.microsoftonline.com/Common/oauth2/token可以使用,但更推荐替换为你的租户ID对应的端点,避免通用端点的兼容性问题。租户ID可通过以下命令获取:
az account show --query tenantId -o tsv
替换后端点格式为:https://login.microsoftonline.com/{你的租户ID}/oauth2/token
3. fs.azure.account.oauth2.refresh.token
az account get-access-token返回的是访问令牌,无法用于刷新令牌流,必须获取专门的刷新令牌,有两种方法:
方法一:从Azure CLI缓存提取
Azure CLI会将登录后的凭据缓存到本地文件:
- Windows:
%USERPROFILE%\.azure\accessTokens.json - macOS/Linux:
~/.azure/accessTokens.json
打开该文件,找到对应租户的条目,提取其中的refreshToken字段值即可。
方法二:手动OAuth2授权码流程获取
如果缓存文件无法读取或需要更可控的方式,可通过授权码流程获取:
- 构造授权URL(替换
{你的租户ID}):
https://login.microsoftonline.com/{你的租户ID}/oauth2/v2.0/authorize?client_id=04b07795-8ddb-461a-bbee-02f9e1bf7b46&response_type=code&redirect_uri=http://localhost&response_mode=query&scope=https://storage.azure.com/.default offline_access
- 在浏览器打开该URL,登录你的Azure账户,跳转后从URL中提取
code参数(授权码)。 - 用curl发送POST请求获取令牌(替换
{你的租户ID}、{授权码}):
curl -X POST https://login.microsoftonline.com/{你的租户ID}/oauth2/v2.0/token \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "client_id=04b07795-8ddb-461a-bbee-02f9e1bf7b46" \ -d "grant_type=authorization_code" \ -d "code={授权码}" \ -d "redirect_uri=http://localhost" \ -d "scope=https://storage.azure.com/.default offline_access"
从返回的JSON中提取refresh_token字段,即为所需值。
二、完整配置示例
将以下配置添加到Hadoop的core-site.xml或对应配置文件中:
<property> <name>fs.azure.account.auth.type</name> <value>OAuth</value> </property> <property> <name>fs.azure.account.oauth.provider.type</name> <value>org.apache.hadoop.fs.azurebfs.oauth2.RefreshTokenBasedTokenProvider</value> </property> <property> <name>fs.azure.account.oauth2.client.id</name> <value>04b07795-8ddb-461a-bbee-02f9e1bf7b46</value> </property> <property> <name>fs.azure.account.oauth2.refresh.endpoint</name> <value>https://login.microsoftonline.com/{你的租户ID}/oauth2/token</value> </property> <property> <name>fs.azure.account.oauth2.refresh.token</name> <value>{你的刷新令牌}</value> </property>
三、错误原因分析
你遇到的AADSTS9002313错误,根源是:
- 误用了访问令牌作为刷新令牌参数
- 可能使用了错误的client id(现在替换为Azure CLI的固定ID即可解决)
- 通用端点
Common在部分租户环境下兼容性不佳,换成租户专属端点可避免此类问题
内容的提问来源于stack exchange,提问作者Del
相关产品推荐
相关产品推荐

