Skip to content

Instantly share code, notes, and snippets.

@jlong
Last active April 27, 2016 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlong/300f3dea29907aa31c70 to your computer and use it in GitHub Desktop.
Save jlong/300f3dea29907aa31c70 to your computer and use it in GitHub Desktop.
Using the UserVoice API v2 with a trusted client
require 'net/http'
require 'json'
# For now you must use a trusted API client, never place your API credentials
# for a trusted client in an insecure location. Especially not in JavaScript on
# the client side.
client_key = "YOUR_API_CLIENT_KEY"
client_secret = "YOUR_API_CLIENT_SECRET"
host = "YOUR_SUBDOMAIN.uservoice.com"
# Get an OAuth token
uri = URI("https://#{host}/api/v2/oauth/token")
req = Net::HTTP::Post.new(uri)
req.set_form_data({
grant_type: 'client_credentials',
client_id: client_key,
client_secret: client_secret,
})
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) {|http|
http.request(req)
}
token = JSON.parse(res.body)['access_token']
# Make your request
uri = URI("https://#{host}/api/v2/admin/users/current")
req = Net::HTTP::Get.new(uri)
req['Authorization'] = "Bearer #{token}"
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) {|http|
http.use_ssl = true
http.request(req)
}
puts res.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment