How use Confluence API from C#

Mikael
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 8, 2024

Hello Comunity

Firs, I'm new to this API so assume no prior knowledge. I'm learning.

I intend to create a Confluence page programmatically via API using C#.

I have browsed internet for several days and found some examples aiding, but should there not be a description of the API somewhere?

My code includes "using Confluence.Client;" and I run it in Viaual Stusio Professional.

Based on examples I attempt log in to Confluence by:

public void loginToConfluence()
{
  try
{
  WebRequest request = WebRequest.Create(this.baseURL + this.loginAPI);
  System.String postData = "{\"username\":\"" + this.loginUserName + "\",\"password\":\"" + this.loginPassWord + "\"}";
  byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  request.Method = "POST";
  request.ContentType = "application/json";
  request.ContentLength = byteArray.Length;

  Stream dataStream = request.GetRequestStream();
  dataStream.Write(byteArray, 0, byteArray.Length);
  dataStream.Close();

  WebResponse response = request.GetResponse();
  dataStream = response.GetResponseStream();
  StreamReader reader = new StreamReader(dataStream);
  System.String responseFromServer = reader.ReadToEnd();
  reader.Close();
  dataStream.Close();
  response.Close();

  this.loginResponse = responseFromServer;
  Console.WriteLine("\nloginResopnse:");
  Console.WriteLine(this.loginResponse);

}
  catch (Exception ex)
{
  Console.WriteLine("Error in loginToConfluence: " + ex);
  this.errorsOccurred = true;
}

Responce:

Started
Error in loginToConfluence: System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.GetResponse()
at IntToConfluence.JiraRequest.loginToConfluence() in C:\MLL\C#\GetConfluencePage\Program.cs:line 154
Finished

I understand the access is refused although I can use the same url to open the page in a browser, using the same UI/PW.

Why does the exception refers to JiraRequest in error message?

Is there a different URL to use than the one used by browser to access the page via api?

Any recommended API documentation?

Any simple C# example recommended are greatly appreciated and will aid my learning!

 

1 answer

0 votes
Marc Koppelaar - Devoteam
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 8, 2024

HI @Mikael 

Welcome to the community but via the API you can't use username and password for authentication.

Authentication is done based on username and API key.

Documentation on the Confluence API can be found here; https://developer.atlassian.com/cloud/confluence/rest/v1/intro/#about 

The Jira related message is that Cloud uses a single user administration for all Atlassian related products and permissions to be granted.

This is based upon the Jira toolset, thats why it relates to Jira, even if you don't use or have Jira.

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events