我想从Java API(auth0客户端)获取Auth0用户列表。我可以做吗?基于Java + Spring Security的API。 我试图用RestTemplate做到这一点:
List findAllUsers(){
String idToken = RestService.getIdToken()
HttpHeaders headers = new HttpHeaders()
headers.set("Authorization", "Bearer $idToken");
List users = []
try{
ResponseEntity entity = restTemplate.exchange(issuer+"api/v2/users", HttpMethod.GET, new HttpEntity<Object>(headers), List)
users = entity.getBody()
} catch (HttpClientErrorException e){
e.printStackTrace()
}
return users
}
但我获得了403 Forbidden状态。
答案 0 :(得分:4)
是的,这是可能的。但是,您需要使用Auth0 Management Token
。
请参阅我编写的this sample,我在哪里进行搜索(在这种情况下,过滤以获取特定用户帐户而不是所有用户)。
您可以获取管理令牌并查看搜索选项here。您需要根据文档提供管理令牌read:users
和read:user_idp_tokens
范围。
建议您首先使用Postman之类的工具来获取正确的搜索,然后转换为Java。您可以使用OkHttpClient
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.2.0</version>
</dependency>
此外,Postman也可用于generate
您定义的请求中的代码。 Auth0目前不提供Java库的管理API,但很快就会这样做。
<强>更新强>
这是一些有效的代码,只需替换您的管理令牌和租户(用于URL)。
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
/**
* Created by arcseldon on 07/09/2016.
*/
public class GetUsers {
public static void main(String[] args) throws IOException {
final String MANAGEMENT_TOKEN = "YOUR_MANAGEMENT_TOKEN";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://<YOUR_TENANT>.auth0.com/api/v2/users")
.get()
.addHeader("authorization", "Bearer " + MANAGEMENT_TOKEN)
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
System.out.println("All done: " + response.body().string());
}
}
答案 1 :(得分:3)
您可以使用Library Apache HTTP Client:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
并且此代码可以正常工作:
HttpClient httpclient = new DefaultHttpClient();
URIBuilder builder = new URIBuilder("https://domain.auth0.com/api/v2/users");
HttpGet httpGet = new HttpGet(builder.build());
httpGet.setHeader("Accept", "application/json");
httpGet.setHeader("Content-type", "application/json");
httpGet.setHeader(
"Authorization",
"Bearer "+ token);
HttpResponse responseHttp = httpclient.execute(httpGet);
HttpEntity entity = responseHttp.getEntity();
BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
System.out.println(line);
}
此代码仅为您提供50位用户,如果您的用户超过50位,则必须使用此网址:
https://domain.auth0.com/api/v2/users?per_page=50&page="+ pageNumber + "&include_totals=true"
您可以指定每页的用户数和页码,包括用户总数。
此代码的结果是JSON,因此使用Gson库解析它的最简单方法是:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
以下是使用Gson Library解析Json字符串的示例:
public String parse(String jsonLine) {
JsonElement jelement = new JsonParser().parse(jsonLine);
JsonObject jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("data");
JsonArray jarray = jobject.getAsJsonArray("translations");
jobject = jarray.get(0).getAsJsonObject();
String result = jobject.get("translatedText").toString();
return result;
}
答案 2 :(得分:0)
使用休息模板的简单方法。
final String MANAGEMENT_TOKEN = "ManagementToken";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Authorization", "Bearer" + " " + MANAGEMENT_TOKEN);
HttpEntity<Object> httpEntity = new HttpEntity<>(httpHeaders);
String url = "https:<//YOUR_DOMAIN>.auth0.com/api/v2/users";
ResponseEntity<Object>response=restTemplate.exchange(url,HttpMethod.GET,httpEntity, Object.class);
`
希望这会帮助你