Last active
April 27, 2016 16:48
-
-
Save jlong/300f3dea29907aa31c70 to your computer and use it in GitHub Desktop.
Using the UserVoice API v2 with a trusted client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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