今天在处理接口的时候发现 参数正确 地址正确 却提示: invalid_scope
旧版的identityserver4 没有这个错误 时代在变 代码用法也在不断的更新。
今天就来记录下 出现 invalid_scope 怎么处理?
在使用新版的 identityserver4 的时候出现了这个错误:

这个坑浪费了我很多时间,记录下 避免更多的人遇到这个坑。
服务端提示这个错误如图:

这个就是"error": "invalid_scope"
接下来我们怎么来解决呢?
第一步:
源配置代码如下:
public static IEnumerable<ApiResource> GetApiScopes()
{
//可访问的API资源(资源名,资源描述)
return new List<ApiResource>
{
new ApiResource("FirstApi", "第一个服务"),
};
}修改后的代码:
public static IEnumerable<ApiResource> GetApiScopes()
{
//可访问的API资源(资源名,资源描述)
return new List<ApiResource>
{
new ApiResource("FirstApi", "第一个服务") {
Scopes ={"FirstApi"},//不配置就返回 invalid_scope
}
};
}第二步:
在配置文件中添加以下代码:
public static IEnumerable<ApiScope> ApiScopes =>
new ApiScope[]
{
new ApiScope("FirstApi")
};第三步:
在配置文件中添加以下代码:
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(), //未添加导致scope错误
new IdentityResources.Profile()
};
}第四步:
在Startup ->ConfigureServices 中添加如下配置
.AddInMemoryIdentityResources(IdentityConfig.GetIdentityResources()) .AddInMemoryApiScopes(IdentityConfig.ApiScopes);//添加

参数如下(http://127.0.0.1:9500/connect/token)
grant_type client_credentials client_id ClientPc client_secret 123456
这样添加完成以后 我们再来看看我们调用接口试试:

这样就可以使用了 。
源码地址:https://cyimt.net/Download/Download?Down=101526535848






赞









