Skip to content

Instantly share code, notes, and snippets.

@jlong
Last active Apr 27, 2016
Embed
What would you like to do?
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