简体   繁体   English

Java中的Curl oAuth等效项

[英]Curl oAuth equivalent in Java

I can't find the way to reproduce the following curl oAuth authentication call in Java: 我找不到在Java中重现以下curl OAuth身份验证调用的方法:

curl 'https://id.herokuapp.com/oauth/token' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: application/json' -H 'Authorization: Basic AUTH_VALUE' -H 'Connection: keep-alive'  --data 'username=_USERNAME&password=_PASSWORD&grant_type=password&scope=read%20write&client_secret=_SECRET&client_id=_CLIENT_ID' --compressed

I don't know how to pass the --data value to the call. 我不知道如何将--data值传递给调用。

If you're using standard java.net.URL it can be done but the syntax is rather cumbersome, I suggest that you try using HTTP Components library . 如果使用标准的java.net.URL可以完成,但是语法相当繁琐,建议您尝试使用HTTP Components库 You should end up with something like this: 您应该以如下形式结束:

final HttpUriRequest request = RequestBuilder.get()
        .setUri("https://id.herokuapp.com/oauth/token")
        .setHeader("Content-Type", "application/x-www-form-urlencoded")
        .setHeader("Accept", "application/json")
        .setHeader("Authorization", "Basic AUTH_VALUE")
        .addParameter("username", "_USERNAME")
        .addParameter("password", "_PASSWORD")
        .addParameter("grant_type", "password")
        .addParameter("scope", "read write")
        .addParameter("client_secret", "_SECRET")
        .addParameter("client_id", "_CLIENT_ID")
        .build();

final HttpClient client = HttpClientBuilder.create().build();
final HttpResponse response = client.execute(request);
final HttpEntity entity = response.getEntity();

System.out.println(EntityUtils.toString(entity)); // or whatever processing you need

GZip/deflate and keep alive handling is provided out of the box if the HttpClient is created using HttpClientBuilder. 如果使用HttpClientBuilder创建HttpClient,则开箱即可提供GZip / deflate和保持活动处理功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM