OAuth ruby examples

As part of the set of OAuth examples, here are some basic examples in ruby, using the ruby OAuth gem. Feel free to improve. Since they are of no use themselves, it doesn't make much sense to run them! If you need any help, please ask away either on dev@openstreetmap.org or contact Gravitystorm

Registering and Authorising your application

#!/usr/bin/ruby# Get all the auth details you need# You wouldn't actually do it this way, but hey.# Normally you'd distribute the consumer stuff with your# application, and each user gets the access_token stuff# But hey, this is just a demo.require'rubygems'require'oauth'require'yaml'# Format of auth.yml:# consumer_key: (from osm.org)# consumer_secret: (from osm.org)# token: (use oauth setup flow to get this)# token_secret: (use oauth setup flow to get this)auth={}puts"First, go register a new application at "puts"http://api06.dev.openstreetmap.org/oauth_clients/new"puts"Tick the appropriate boxes"puts"Enter the consumer key you are assigned:"auth["consumer_key"]=gets.stripputs"Enter the consumer secret you are assigned:"auth["consumer_secret"]=gets.stripputs"Your application is now set up, but you need to register"puts"this instance of it with your user account."@consumer=OAuth::Consumer.newauth["consumer_key"],auth["consumer_secret"],{:site=>"http://api06.dev.openstreetmap.org"}@request_token=@consumer.get_request_tokenputs"Visit the following URL, log in if you need to, and authorize the app"puts@request_token.authorize_urlputs"When you've authorized that token, enter the verifier code you are assigned:"verifier=gets.stripputs"Converting request token into access token..."@access_token=@request_token.get_access_token(:oauth_verifier=>verifier)auth["token"]=@access_token.tokenauth["token_secret"]=@access_token.secretFile.open('auth.yaml','w'){|f|YAML.dump(auth,f)}puts"Done. Have a look at auth.yaml to see what's there."

Making basic HTTP requests

#!/usr/bin/ruby# Simple OSM Auth example showing GET PUT and DELETE methods# Requires OAuth rubygemrequire'rubygems'require'oauth'require'date'require'yaml'# Format of auth.yml:# consumer_key: (from osm.org)# consumer_secret: (from osm.org)# token: (use oauth setup flow to get this)# token_secret: (use oauth setup flow to get this)auth=YAML.load(File.open('auth.yaml'))# The consumer key and consumer secret are the identifiers for this particular application, and are # issued when the application is registered with the site. Use your own.@consumer=OAuth::Consumer.newauth['consumer_key'],auth['consumer_secret'],{:site=>"http://api06.dev.openstreetmap.org"}# Create the access_token for all traffic@access_token=OAuth::AccessToken.new(@consumer,auth['token'],auth['token_secret'])# Use the access token for various commands. Although these take plain strings, other API methods # will take XML documents.@access_token.put('/api/0.6/user/preferences/demo_last_run_time',DateTime.now().to_s,{'Content-Type'=>'text/plain'})@access_token.put('/api/0.6/user/preferences/deleteme',"This shouldn't be seen",{'Content-Type'=>'text/plain'})@access_token.delete('/api/0.6/user/preferences/deleteme')puts@access_token.get('/api/0.6/user/preferences').body

Multipart form uploads

One of the harder things to do is uploading GPX files, since that requires a multipart/form-data POST. The OAuth gem doesn't (appear to) support this, so the request has to be hand-constructed and then signed by the access token. This might change in future versions.