我正在尝试获取已发布 API 之一的 OAuth 访问令牌。我已经为 API 资源定义了本地范围“XYZ_Scope”。当我进入 DevPortal 时,我可以使用 UI 生成范围定义的范围,但不能使用 curl 命令。
curl 命令如下所示:
curl --location 'https://{your-ip-address}:9443/oauth2/token' \
--header 'Authorization: Basic Base64(consumer-key:consumer-secret)' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=username' \
--data-urlencode 'password=password'
以下是响应的样子:
{
"access_token": "*token*",
"scope": "default",
"token_type": "Bearer",
"expires_in": 3600
}
我无法使用访问令牌访问已发布的 API。这是我面临的问题:
{
"code": "900910",
"message": "The access token does not allow you to access the requested resource",
"description": "User is NOT authorized to access the Resource: /{device-name}/{resource-name}. Scope validation failed."
}
我是否可以将本地范围添加到curl命令中以获取访问令牌?
谢谢您的回答。
在令牌调用中,您必须指定令牌的必要范围,如下所示。
curl --location 'https://{your-ip-address}:9443/oauth2/token' \
--header 'Authorization: Basic Base64(consumer-key:consumer-secret)' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=username' \
--data-urlencode 'password=password' \
--data-urlencode 'scope=XYZ_Scope'
如果您需要请求具有多个范围的令牌,则必须以空格分隔的方式指定它们,如下所示。
curl --location 'https://{your-ip-address}:9443/oauth2/token' \
--header 'Authorization: Basic Base64(consumer-key:consumer-secret)' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=username' \
--data-urlencode 'password=password' \
--data-urlencode 'scope=ABC_scope XYZ_Scope'