Confluence REST API 总是调用 return 404 - 未找到(身份验证问题?)
Confluence REST API call always return 404 - Not found (Authentication issue?)
编辑:
好的,我发现这绝对是一个身份验证问题:
这个简短的 Python 脚本可以很好地完成任务:
import requests
from requests.auth import HTTPBasicAuth
URL = "https://wiki.myCompany.corp/confluence-dev/rest/api/content/180158489";
r = requests.get(URL, auth = HTTPBasicAuth('myUser', 'myPass'), verify=False)
r.raise_for_status()
print(r.json())
为什么它不适用于 Java?
初始问题:
我正在尝试使用 Apache httpclient/httpcore 编写一个简单的 Http 请求来获取 Confluence 页面的内容,但我得到的只是状态 404。
这是我的代码:
String url = "https://wiki.mycompany.corp/confluence-dev/rest/api/content/180158489";
HttpHost proxy = new HttpHost("proxy.myCompany.at", 57165);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("myUsername", "myPassword");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultCredentialsProvider(provider);
builder.setRoutePlanner(routePlanner);
HttpClient client = builder.build();
HttpGet get = new HttpGet(url);
HttpResponse r = client.execute(get);
System.out.println(r.getStatusLine().toString());
输出:
HTTP/1.1 404 未找到
当我在浏览器中打开 URL 时,只要我已登录,我就会返回正确的汇合 JSON。当我注销并尝试打开 URL 在浏览器中我得到一个 JSON
{
"statusCode":404,
"data":
{
"authorized":false,
"valid":true,
"allowedInReadOnlyMode":true,
"errors":[],"successful":false
},
"message":"No content found with id: ContentId{id=180158489}",
"reason":"Not Found"
}
所以对我来说,我的代码中的身份验证似乎无法正常工作。
你知道这里可能出了什么问题吗?
好的,我找到了解决方案(不知何故)。
在我的 Java 代码中,身份验证不起作用(无论出于何种原因,我在多个示例中都看到过这样做......)。
无论如何,
不过,如果我手动设置我的 header 是可行的,就像这样:
HttpGet request = new HttpGet(url);
String authorizationString = "Basic " + Base64.getEncoder().encodeToString(("myUser:myPass").getBytes()).replace("\n", "");
request.setHeader(HttpHeaders.AUTHORIZATION, authorizationString);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine().toString());
结果:
HTTP/1.1 200 行
编辑: 好的,我发现这绝对是一个身份验证问题: 这个简短的 Python 脚本可以很好地完成任务:
import requests
from requests.auth import HTTPBasicAuth
URL = "https://wiki.myCompany.corp/confluence-dev/rest/api/content/180158489";
r = requests.get(URL, auth = HTTPBasicAuth('myUser', 'myPass'), verify=False)
r.raise_for_status()
print(r.json())
为什么它不适用于 Java?
初始问题:
我正在尝试使用 Apache httpclient/httpcore 编写一个简单的 Http 请求来获取 Confluence 页面的内容,但我得到的只是状态 404。
这是我的代码:
String url = "https://wiki.mycompany.corp/confluence-dev/rest/api/content/180158489";
HttpHost proxy = new HttpHost("proxy.myCompany.at", 57165);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("myUsername", "myPassword");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultCredentialsProvider(provider);
builder.setRoutePlanner(routePlanner);
HttpClient client = builder.build();
HttpGet get = new HttpGet(url);
HttpResponse r = client.execute(get);
System.out.println(r.getStatusLine().toString());
输出: HTTP/1.1 404 未找到
当我在浏览器中打开 URL 时,只要我已登录,我就会返回正确的汇合 JSON。当我注销并尝试打开 URL 在浏览器中我得到一个 JSON
{
"statusCode":404,
"data":
{
"authorized":false,
"valid":true,
"allowedInReadOnlyMode":true,
"errors":[],"successful":false
},
"message":"No content found with id: ContentId{id=180158489}",
"reason":"Not Found"
}
所以对我来说,我的代码中的身份验证似乎无法正常工作。
你知道这里可能出了什么问题吗?
好的,我找到了解决方案(不知何故)。 在我的 Java 代码中,身份验证不起作用(无论出于何种原因,我在多个示例中都看到过这样做......)。
无论如何, 不过,如果我手动设置我的 header 是可行的,就像这样:
HttpGet request = new HttpGet(url);
String authorizationString = "Basic " + Base64.getEncoder().encodeToString(("myUser:myPass").getBytes()).replace("\n", "");
request.setHeader(HttpHeaders.AUTHORIZATION, authorizationString);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine().toString());
结果:
HTTP/1.1 200 行