Skip to content

Instantly share code, notes, and snippets.

@jlong
Created May 24, 2012 12:41
Show Gist options
  • Save jlong/2781361 to your computer and use it in GitHub Desktop.
Save jlong/2781361 to your computer and use it in GitHub Desktop.
Creating a user with the UserVoice API
#!/usr/bin/env ruby
require 'rubygems'
require "oauth"
require 'json'
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
# Trusted Client
KEY = 'CLIENT_KEY'
SECRET = 'CLIENT_SECRET'
SITE = 'http://your_subdomain.uservoice.com'
consumer = OAuth::Consumer.new(KEY, SECRET)
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"]))
user = {
"user[display_name]" => 'Scott Test',
"user[email]" => "[email protected]",
"request_token" => request_token.token
}
response = consumer.request(:post, "#{SITE}/api/v1/users.json", nil, {}, user)
user_hash = JSON.parse(response.body)
puts user_hash
access_token = OAuth::AccessToken.from_hash(consumer, symbolize_hash(user_hash["token"]))
puts "Create user: #{user.inspect}"
puts "Obtained accessToken: #{access_token.inspect}"
puts "Key: #{access_token.token}"
puts "Secret: #{access_token.secret}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment