Developer Blog

API - Creating and Listing Tickets

in API, Public

Here is another example using our API. This one shows how to create a user from an SSO token, then use that user to create a ticket. Finally we will then list out the tickets.

# helper method
def symbolize_hash(hash)
  symbolized_hash = {}  
  # puts hash.inspect
  
  hash.each do |key, value|
    symbolized_hash[key.to_sym] = value
  end
  return symbolized_hash
end

consumer = OAuth::Consumer.new(KEY, SECRET)

puts "Get request token"
response = consumer.request(:get, "#{SITE}/api/v1/oauth/request_token.json")
request_token_hash = JSON.parse(response.body)
puts request_token_hash

request_token = OAuth::RequestToken.from_hash(consumer, 
   symbolize_hash(request_token_hash["token"]))

# this is just a library we use to create SSO tokens, similar code 
# snippets can be found under the SSO docs here
encoder = Sso::Schemes::AesCbc128::Base64Encoder.new(SUBDOMAIN, API_KEY)

# create the SSO token setting the user to be an admin so 
# we can list the tickets back later
sso_token = encoder.process({ 
  :guid => "test@test.com",
  :expires => "2012-12-31",
  :email => "test@test.com",
  :avatar_url => "http://external.com/users/1.png",
  :display_name => "Testie McTest",
  :admin => "accept"
})

# get the access token for this new user
response = consumer.request(:post, "#{SITE}/api/v1/oauth/authorize.json", nil, {}, {
  :sso => sso_token, :request_token => request_token_hash["token"]["oauth_token"]
})
user_hash = JSON.parse(response.body)
access_token = OAuth::AccessToken.from_hash(consumer, symbolize_hash(user_hash["token"]))

# create the params for the ticket
ticket = {
  "ticket[message]" => "Everything is broken......",
  "ticket[subject]" => "Help!!!"
}

# create a ticket
response = access_token.request(:post, "#{SITE}/api/v1/tickets.json", ticket)
body = JSON.parse(response.body)

# get our ticket 
response = access_token.request(:get, "#{SITE}/api/v1/tickets.json?per_page=1")
body = JSON.parse(response.body)

puts body["tickets"][0].inspect

~ Scott


Like us on Facebook Join us on Google+
blog comments powered by Disqus
product screenshot

Create a UserVoice account

Discover how easy it can be to provide great customer service

Try it for free