为了访问Confluence的REST API,需要使用OAuth凭据。OAuth是一种授权机制,允许用户授权访问他们的Confluence数据,而无需共享他们的用户名和密码。
以下是使用Java和Atlassian SDK设置Confluence OAuth凭据的示例:
import com.atlassian.oauth.client.OAuthClient;
import com.atlassian.oauth.client.OAuthClientImpl;
import com.atlassian.oauth.consumer.ConsumerService;
import com.atlassian.oauth.consumer.ConsumerServiceFactory;
import com.atlassian.oauth.consumer.ConsumerToken;
import com.atlassian.oauth.consumer.ConsumerTokenSecret;
ConsumerService consumerService = new ConsumerServiceFactory().createService(new OAuthConfigBuilder()
.requestTokenUrl(confluenceBaseUrl + "/plugins/servlet/oauth/request-token")
.accessTokenUrl(confluenceBaseUrl + "/plugins/servlet/oauth/access-token")
.authorizeUrl(confluenceBaseUrl + "/plugins/servlet/oauth/authorize")
.applicationName("My Confluence Application")
.consumerKey(consumerKey)
.privateKey(privateKey)
.build());
String authorizationUrl = consumerService.getAuthorizationUrl(new ConsumerToken(consumerKey, consumerSecret), callbackUrl);
// This is the URL the user will be redirected to after they authorize the request.
// Your application should listen for requests on this URL and use the verifier
// to get an access token and token secret for each user.
callbackUrl + "?oauth_token=" + requestToken.getToken().toString() + "&oauth_verifier=" + verifier
此代码将获取Confluence基本的OAuth凭据并定制它们以适合您的应用程序。 您将需要将confluenceBaseUrl
,consumerKey
,consumerSecret
和 privateKey
的值替换为您自己的值。
一旦您有了OAuth凭据,您可以使用它们来访问Confluence REST API。 以下是使用Java的Atlassian SDK访问Confluence REST API的示例:
import com.atlassian.sal.api.net.Request;
import com.atlassian.sal.api.net.ResponseException;
import com.atlassian.sal.api.net.ResponseHandler;
import com.atlassian.sal.api.net.ReturningResponseHandler;