我想实现auth0端点以获取用户,但我无法弄清楚如何正确执行此操作。
我现在所做的一切 - 我创建了auth0网关:
public class Auth0Gateway implements AuthGateway {
private ManagementAPI mgmt;
private final Logger logger;
private final DbService dbService;
@Inject
public Auth0Gateway(Logger logger, DbService dbService) {
this.logger = logger;
this.dbService = dbService;
mgmt = new ManagementAPI(System.getenv(EnvVars.AUTH0_DOMAIN.name()), this.getAuthToken());
}
@Override
public List<FPUser> getUsers() {
UserFilter userFilter = new UserFilter();
Request<UsersPage> request = mgmt.users().list(userFilter);
try {
List<User> users = request.execute().getItems();
List<FPUser> fpUsers = users.stream().map(FPUser::new).collect(Collectors.toList());
return fpUsers;
} catch (APIException exception) {
// api error
logger.error(exception.getMessage());
} catch (Auth0Exception exception) {
// request error
logger.error(exception.getMessage());
}
return null;
}
@Override
public FPUser getUser(String id) {
UserFilter userFilter = new UserFilter();
User user = null;
try {
user = mgmt.users().get(id, userFilter).execute();
} catch (APIException exception) {
// api error
logger.error(exception.getMessage());
} catch (Auth0Exception exception) {
// request error
logger.error(exception.getMessage());
}
if (user != null)
return new FPUser(user);
return null;
}
private String getAuthToken() {
Cache cache = dbService.get(Cache.class, "AUTH0_API_TOKEN");
return cache.getCacheValue();
}
public void refreshApiToken() {
AuthAPI auth = new AuthAPI(System.getenv(EnvVars.AUTH0_DOMAIN.name()), System.getenv(EnvVars.AUTH0_CLIENT_ID.name()), System.getenv(EnvVars.AUTH0_CLIENT_SECRET.name()));
try {
String apiToken = auth.requestToken(System.getenv(EnvVars.AUTH0_AUDIENCE.name())).execute().getAccessToken();
Cache cache = new Cache();
cache.setId("AUTH0_API_TOKEN");
cache.setApiToken(apiToken);
dbService.save(cache);
if (apiToken != null)
logger.log("Refreshed api token");
else
logger.warning("Api token is empty");
} catch (Auth0Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
}
我有PersonalData类来存储有关User的信息。
我现在要做的是调用此端点来获取用户:https://auth0.com/docs/api/management/v2#!/Users/get_users_by_id但我以前从未这样做过。有人可以帮我这个吗?