We are in the process of automating the uploading of avatars for users in our Jira Service Desk (Server, not Cloud). We use PowerShell for most of our integrations with Jira, and it works fine. However, some of the APIs throw an XSRF check failed error. I've confirmed that I'm using the same base URL to access the API as we have set in the Base URL. I have the header X-Atlassian-Token set to nocheck. Is it not possible to call these APIs outside of the JIRA web UI? Code below for clarity.
Actual server response is (403) Forbidden. The response body is XSRF check failed.
Try
{
[String] $APIAvatarURL = "{@JiraBaseURL}rest/api/latest/user/avatar/temporary?username={@SAMAccountName}&filename={@FileName}"
$APIAvatarURL = $APIAvatarURL.Replace("{@JiraBaseURL}", $JiraSettings["JiraBaseURL"]);
$APIAvatarURL = $APIAvatarURL.Replace("{@SAMAccountName}", $SAMAccountName);
$APIAvatarURL = $APIAvatarURL.Replace("{@FileName}", $FileName);
[Net.HttpWebRequest] $HttpWebRequest = [Net.WebRequest]::Create($APIAvatarURL);
$HttpWebRequest.Method = "POST";
$HttpWebRequest.KeepAlive = $false;
[void] $HttpWebRequest.Headers.Add("Authorization", $JiraSettings["Authorization"]);
[void] $HttpWebRequest.Headers.Add("X-Atlassian-Token", "nocheck") ;
$HttpWebRequest.ContentType = "image/jpeg";
$HttpWebRequest.ContentLength = $PhotoBinary.Length;
[IO.Stream] $RequestStream = $HttpWebRequest.GetRequestStream();
[void] $RequestStream.Write($PhotoBinary, 0, $PhotoBinary.Length);
[void] $RequestStream.Close();
[Net.HttpWebResponse] $HttpWebResponse = $HttpWebRequest.GetResponse();
[IO.Stream] $ResponseStream = $HttpWebResponse.GetResponseStream();
[IO.StreamReader] $StreamReader = [IO.StreamReader]::new($ResponseStream);
[String] $ResponseString = $StreamReader.ReadToEnd();
$StreamReader.Dispose();
$ResponseStream.Dispose();
$RequestStream.Dispose();
$HttpWebResponse.Dispose();
}
Catch [Net.WebException]
{
[String] $Message = $_.Exception.Message;
[IO.StreamReader] $ExceptionStreamReader = [IO.StreamReader]::new($_.Exception.Response.GetResponseStream());
$Message += "`n" + $ExceptionStreamReader.ReadToEnd();
$ExceptionStreamReader.Dispose();
Write-Host -Object $Message -ForegroundColor Red;
}