REST API authentication changes - .NET/PowerShell

Answered

7 Comments

  • Matt McGrath
    Comment actions Permalink

    Thanks also to Alex Perez from Zendesk Support who pointed us in the right direction!

    0
  • Matt McGrath
    Comment actions Permalink

    Our developers were horrified that I said we couldn't get the splatting to work properly. I meant to say 'at the time'... We now have it working correctly :)

    0
  • Maximilian Kless
    Comment actions Permalink

    Thanks for this! It didn't work for me at first (perhaps there was another update to the API) but I got it working by using "/token:" instead of ":" in the headers.

    So, my params object is:

    $params = @{

                    Uri = " https://whatever.zendesk.com/api/v2/tickets.json";

                    Method = 'Get';

                    Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($username)/token:$($token)"));}

               }

    0
  • JD Mills
    Comment actions Permalink

    Thanks all, I was able to work token authentication into my own scripts using the examples you provided. Here's an example of a quick PowerShell script that returns a ticket using an ID you specify. I hope it helps someone.

    param(
    [Int32]$ticketID
    )

    $headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($env:ZendeskAPI_Username):$($env:ZendeskAPI_Token)"));}
    #where $env:ZendeskAPI_Username and $env:ZendeskAPI_Token are system environmental variables.
    #where $env:ZendeskAPI_Username is set to apiuser@yourdomain.com/token.

    if(!($ticketID)){
    $ticketID = Read-Host "What is the Zendesk ticket number?"
    }

    $ticketsURI = "https://yourdomain.zendesk.com/api/v2/tickets/$ticketID.json"

    $ticket = Invoke-RestMethod -Uri $ticketsURI -Method Get -Headers $headers -ContentType "application/json"

    $ticket = $ticket.ticket

    return $ticket
    0
  • Peter Hochstrasser
    Comment actions Permalink

    A short notice to PS Users: Since about May or June 2018, the script above will not authenticate. The reason is TLS - while Windows apparently defaults to 1.0, Zendesk wants to use 1.2.

    The error you'll get is something like this:

    The underlying connection was closed. An unexpected error occurred on a send.

    The correction is simple, once you find the solution in a post like this one: 

    https://stackoverflow.com/questions/41897114/unexpected-error-occurred-running-a-simple-unauthorized-rest-query

     

    Simply add the line 

      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;

    before calling the Invoke-RestMethod commandlet, and all works as before.

    P.

    2
  • Joey
    Comment actions Permalink

    Thanks for the tip, Peter!

    0
  • Rlindstrom
    Comment actions Permalink

    Thanks for the help.  I had to add the following line to the C# code in my CLR to use TLS 1.2 to communicate with the REST API.

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;

    Once this line was added before the WebRequest was issued the API calls worked again.

    1

Post is closed for comments.

Powered by Zendesk