API: Example
An example of a Ruby script that does a full access of the API and retrieves some of the member's Campaign data.
Use this as an example of how the code would work in your preferred programming language.
#!/usr/bin/env ruby
require 'oauth'
require 'json'
require 'net/http'
require 'cgi'
# OAuth credentials
CONSUMER_KEY = '*** YOUR CONSUMER_KEY ***'
CONSUMER_SECRET = '*** YOUR CONSUMER_SECRET ***'
AUTHORIZE_URL = 'https://www.obsidianportal.com/oauth/authorize'
API_BASE = 'https://api.obsidianportal.com/v1'
# Step 1: Get request token
consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, {
:site => 'https://www.obsidianportal.com',
:request_token_path => '/oauth/request_token',
:access_token_path => '/oauth/access_token',
:authorize_path => '/oauth/authorize',
:signature_method => 'HMAC-SHA1'
})
request_token = consumer.get_request_token
params = request_token.params.map { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
authorize_url = "#{AUTHORIZE_URL}?#{params}"
puts "Go to this URL to authorize: #{authorize_url}"
puts "Enter the PIN/code from the page: "
pin = gets.strip
# Step 2: Exchange for access token
access_token = request_token.get_access_token(
:oauth_verifier => pin
)
puts "Access token obtained: #{access_token.token}"
puts "Token secret: #{access_token.secret}"
# Step 3: Get campaigns list
response = access_token.get("#{API_BASE}/users/me")
campaigns = JSON.parse(response.body)['campaigns']
if campaigns.empty? || !campaigns.is_a?(Array)
puts "No campaigns found or unexpected response format"
puts "Response: #{response.body}"
exit 1
end
first_campaign = campaigns.first
campaign_id = first_campaign['id']
puts "First campaign: #{first_campaign['name']} (ID: #{campaign_id})"
# Step 4: Get first wiki page from that campaign
wiki_response = access_token.get("#{API_BASE}/campaigns/#{campaign_id}/wikis")
wikis = JSON.parse(wiki_response.body)
if wikis.empty? || !wikis.is_a?(Array)
puts "No wiki pages found"
puts "Wiki response: #{wiki_response.body}"
exit 1
end
first_wiki = wikis.first
wiki_id = first_wiki['id']
wiki_slug = first_wiki['slug']
wiki_name = first_wiki['name']
puts "First wiki page: #{wiki_name} (ID: #{wiki_id}, slug: #{wiki_slug})"
# Step 5: Get full content of first wiki page
content_response = access_token.get("#{API_BASE}/campaigns/#{campaign_id}/wikis/#{wiki_id}")
wiki_content = JSON.parse(content_response.body)
puts "\n=== WIKI PAGE CONTENT ==="
puts "Title: #{wiki_content['name']}"
puts "Body: #{wiki_content['body']}"
puts "Body HTML: #{wiki_content['body_html']}"
puts "========================"