【发布时间】:2020-10-15 18:43:21
【问题描述】:
我必须调用 url 来获取访问令牌以调用后续 API。此令牌端点由basic authentication 保护。
token endpoint:- https://xxxxxx.identity.c9dev2.oc9qadev.com/oauth2/v1/token
username: "xxx-ccc"
password: "avcdada"
grant_type: client_credentials
scope: https://xxxxxx.digitalassistant.oci.oc-test.com/api/v1
request type: POST
我无法在 java 中使用上述内容。我尝试了很多代码,但没有一个工作。可以吗,请帮我一些链接。我是java 的新手,并且很挣扎。下面是我使用的代码。它不工作。
package postapicall;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class PostApi1 {
public static void main(String []args)
{
try
{
String authorization = "";
String url= "https://idcs-82972921e42641b1bf08128c3d93a19c.identity.c9dev2.oc9qadev.com/oauth2/v1/token";
String username = "idcs-oda-9417f93560b94eb8a2e2a4c9aac9a3ff-t0_APPID";
String password = "244ae8e2-6f71-4af2-b5cc-9110890d1456";
URL address = new URL(url);
HttpURLConnection hc = (HttpURLConnection) address.openConnection();
hc.setDoOutput(true);
hc.setDoInput(true);
hc.setUseCaches(false);
if (username != null && password != null) {
authorization = username + ":" + password;
}
if (authorization != null) {
byte[] encodedBytes;
encodedBytes = Base64.encode(authorization.getBytes(), 0);
authorization = "Basic " + encodedBytes;
hc.setRequestProperty("Authorization", authorization);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
【问题讨论】:
标签: java rest access-token spring-security-oauth2 bearer-token