Created
March 30, 2012 17:00
-
-
Save h4ck3rm1k3/2252931 to your computer and use it in GitHub Desktop.
OSMDevOAuthClientExample
This file contains 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
use strict; | |
use warnings; | |
use Net::OAuth::RequestTokenRequest; | |
my $oarequest = Net::OAuth->request("access token")->from_hash( | |
{ | |
'oauth_token'=>'a8ea803017f85ed89f7a', | |
'callback'=>'oob', | |
}, | |
'consumer_secret' => 'jJs8XFPjywSyrVlwkNvLanh7ZVyHaJWTQHWFOn1U', | |
'consumer_key' => 'jJs8XFPjywSyrVlwkNvLanh7ZVyHaJWTQHWFOn1U', | |
'token_secret' => 'jJs8XFPjywSyrVlwkNvLanh7ZfsdfsfdVyHaJWTQHWFOn1U', | |
'request_method' => 'GET', | |
'timestamp' => '1333185703', | |
'nonce' => 'F2EBB736-C47E-BF79-5A65-680F6D2DC424', | |
'signature_method' => 'HMAC-SHA1', | |
'request_url' => 'http://pine02.fosm.org/FOSM-Api/OSM-API-Proxy/public/dispatch.cgi/oauth/request_token', | |
); | |
my $output = $oarequest->to_post_body; | |
warn "check output:". $output; |
This file contains 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
use Net::OAuth::RequestTokenRequest; | |
my %args=( | |
'consumer_key' => 'yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ', | |
'consumer_secret' => 'jJs8XFPjywSyrVlwkNvLanh7ZVyHaJWTQHWFOn1U', | |
'extra_params' => {}, | |
'nonce' => 'F2EBB736-C47E-BF79-5A65-680F6D2DC424', | |
'request_method' => 'GET', | |
'request_url' => 'http://pine02.fosm.org/FOSM-Api/OSM-API-Proxy/public/dispatch.cgi/oauth/request_token', | |
'signature' => '5A32qbq4D/ppZ2r9I+jwQlramBc=', | |
'signature_method' => 'HMAC-SHA1', | |
'timestamp' => '1333185703' | |
); | |
my $oarequest = Net::OAuth::RequestTokenRequest->new( | |
%args | |
); | |
if (! $oarequest->verify) | |
{ | |
warn "failed"; | |
} |
This file contains 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
use Net::OAuth::RequestTokenRequest; | |
my $oarequest = Net::OAuth->request("request token")->from_hash( | |
{ | |
'oauth_consumer_key' => 'yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ', | |
'oauth_nonce' => 'F2EBB736-C47E-BF79-5A65-680F6D2DC424', | |
'oauth_signature' => '5A32qbq4D/ppZ2r9I+jwQlramBc=', | |
'oauth_timestamp' => '1333185703', | |
'oauth_signature_method' => 'HMAC-SHA1', | |
}, | |
'consumer_secret' => 'jJs8XFPjywSyrVlwkNvLanh7ZVyHaJWTQHWFOn1U', | |
'request_method' => 'GET', | |
'request_url' => 'http://pine02.fosm.org/FOSM-Api/OSM-API-Proxy/public/dispatch.cgi/oauth/request_token', | |
); | |
if (! $oarequest->verify) | |
{ | |
warn "failed"; | |
} |
This file contains 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
#!perl -w | |
use strict; | |
use lib qw(lib); | |
#use JSON::Any; | |
#put this in the .osmdev_config to start it up | |
#consumer_key = yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ | |
#consumer_secret = jJs8XFPjywSyrVlwkNvLanh7ZVyHaJWTQHWFOn1U | |
my $CONFIG = ".osmdev_config"; | |
binmode STDOUT, ":utf8"; | |
# Get the tokens from the command line, a config file or wherever | |
my %tokens = get_tokens(); | |
my $app = OSMDev->new(%tokens); | |
# Check to see we have a consumer key and secret | |
unless ($app->consumer_key && $app->consumer_secret) { | |
die "You must go get a consumer key and secret from App\n"; | |
} | |
# If the app is authorized (i.e has an access token and secret) | |
# Then look at a restricted resourse | |
get_user_prefs($app) if $app->authorized; | |
# right, we need to get their access stuff | |
print "STEP 1: REQUEST OSM AUTHORIZATION FOR THIS APP\n"; | |
print "\tURL : ".$app->get_authorization_url( callback => 'oob' )."\n"; | |
print "\n-- Please go to the above URL and authorize the app"; | |
print "\n-- It will give you a code. Please type it here: "; | |
my $verifier = <STDIN>; print "\n"; | |
chomp($verifier); $verifier =~ s!(^\s*|\s*$)!!g; | |
$app->verifier($verifier); | |
my ($access_token, $access_token_secret) = $app->request_access_token(); | |
#http://pine02.fosm.org/FOSM-Api/OSM-API-Proxy/public/dispatch.cgi/oauth/request_token?oauth_signature=qvGFD9zKlacy6JMEF8NBzIhbV7U%3D&oauth_timestamp=1333131616&oauth_callback=oob&oauth_nonce=34624276&oauth_consumer_key=yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ&oauth_version=1.0&oauth_signature_method=HMAC-SHA1' | |
#http://pine02.fosm.org/FOSM-Api/OSM-API-Proxy/public/dispatch.cgi/oauth/ | |
#request_token? | |
#oauth_signature=qvGFD9zKlacy6JMEF8NBzIhbV7U%3D& | |
#oauth_timestamp=1333131616& | |
#oauth_callback=oob& | |
#oauth_nonce=34624276& | |
#oauth_consumer_key=yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ& | |
#oauth_version=1.0& | |
#oauth_signature_method=HMAC-SHA1' | |
print "You have now authorized this app.\n"; | |
print "Your access token and secret are:\n\n"; | |
print "access_token=$access_token\n"; | |
print "access_token_secret=$access_token_secret\n"; | |
print "\n"; | |
if (-f $CONFIG) { | |
save_tokens($app); | |
print "You should note these down but they have also been saved in $CONFIG\n\n"; | |
} else { | |
print "You should note these down or put them in $CONFIG with your consumer key and secret\n\n"; | |
} | |
get_user_prefs($app); | |
use YAML; | |
sub get_user_prefs { | |
my $prefs = $app->get_user_prefs; | |
warn Dump($prefs); | |
exit(0); | |
} | |
sub get_tokens { | |
my %tokens = OSMDev->load_tokens($CONFIG); | |
while (@ARGV && $ARGV[0] =~ m!^(\w+)\=(\w+)$!) { | |
$tokens{$1} = $2; | |
shift @ARGV; | |
} | |
return %tokens; | |
} | |
sub save_tokens { | |
my $app = shift; | |
my %tokens = $app->tokens; | |
OSMDev->save_tokens($CONFIG, %tokens); | |
} | |
package OSMDev; | |
use strict; | |
use base qw(Net::OAuth::Simple); | |
sub new { | |
my $class = shift; | |
my %tokens = @_; | |
my $root="http://pine02.fosm.org/FOSM-Api/OSM-API-Proxy/public/dispatch.cgi/"; | |
return $class->SUPER::new( | |
tokens => \%tokens, | |
protocol_version => '1.0a', | |
urls => { | |
authorization_url => $root . 'oauth/authorize', | |
request_token_url => $root . 'oauth/request_token', | |
access_token_url => $root . 'oauth/access_token', | |
# args["api"] = root + "api/0.6/"; | |
# args["policy"] = "http://pine02.fosm.org/api/crossdomain.xml"; | |
# args["connection"] = "XML"; | |
# args["oauth_policy"] = root + "oauth/crossdomain.xml"; | |
# args["oauth_request_url"] = root + "oauth/request_token"; | |
# args["oauth_access_url"] = root + "oauth/access_token"; | |
# args["oauth_auth_url"] = root + "oauth/authorize"; | |
# args["oauth_consumer_key"] = "yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ"; | |
# args["oauth_consumer_secret"] = "jJs8XFPjywSyrVlwkNvLanh7ZVyHaJWTQHWFOn1U"; | |
# args["serverName"] = "pine02.fosm.org"; | |
# args["show_help"] = "once"; | |
# args["site_name"] = "FOSM"; | |
# args["force_auth"] = "force"; | |
}); | |
} | |
sub get_user_prefs { | |
my $self = shift; | |
my $url = "http://api06.dev.openstreetmap.org/api/0.6/user/preferences"; | |
return $self->_make_restricted_request($url, 'GET'); | |
} | |
sub _make_restricted_request { | |
my $self = shift; | |
my $response = $self->make_restricted_request(@_); | |
return $response->content; | |
} | |
1; |
This file contains 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
#!perl -w | |
use strict; | |
use lib qw(lib); | |
#use JSON::Any; | |
#put this in the .osmdev_config to start it up | |
#consumer_key = yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ | |
#consumer_secret = jJs8XFPjywSyrVlwkNvLanh7ZVyHaJWTQHWFOn1U | |
my $CONFIG = ".osmdev_config"; | |
binmode STDOUT, ":utf8"; | |
# Get the tokens from the command line, a config file or wherever | |
my %tokens = get_tokens(); | |
my $app = FOSM->new(%tokens); | |
# Check to see we have a consumer key and secret | |
unless ($app->consumer_key && $app->consumer_secret) { | |
die "You must go get a consumer key and secret from App\n"; | |
} | |
# If the app is authorized (i.e has an access token and secret) | |
# Then look at a restricted resourse | |
get_user_prefs($app) if $app->authorized; | |
# right, we need to get their access stuff | |
print "STEP 1: REQUEST OSM AUTHORIZATION FOR THIS APP\n"; | |
print "\tURL : ".$app->get_authorization_url( callback => 'oob' )."\n"; | |
print "\n-- Please go to the above URL and authorize the app"; | |
print "\n-- It will give you a code. Please type it here: "; | |
my $verifier = <STDIN>; print "\n"; | |
chomp($verifier); $verifier =~ s!(^\s*|\s*$)!!g; | |
$app->verifier($verifier); | |
my ($access_token, $access_token_secret) = $app->request_access_token(); | |
#http://pine02.fosm.org/FOSM-Api/OSM-API-Proxy/public/dispatch.cgi/oauth/request_token?oauth_signature=qvGFD9zKlacy6JMEF8NBzIhbV7U%3D&oauth_timestamp=1333131616&oauth_callback=oob&oauth_nonce=34624276&oauth_consumer_key=yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ&oauth_version=1.0&oauth_signature_method=HMAC-SHA1' | |
#http://pine02.fosm.org/FOSM-Api/OSM-API-Proxy/public/dispatch.cgi/oauth/ | |
#request_token? | |
#oauth_signature=qvGFD9zKlacy6JMEF8NBzIhbV7U%3D& | |
#oauth_timestamp=1333131616& | |
#oauth_callback=oob& | |
#oauth_nonce=34624276& | |
#oauth_consumer_key=yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ& | |
#oauth_version=1.0& | |
#oauth_signature_method=HMAC-SHA1' | |
print "You have now authorized this app.\n"; | |
print "Your access token and secret are:\n\n"; | |
print "access_token=$access_token\n"; | |
print "access_token_secret=$access_token_secret\n"; | |
print "\n"; | |
if (-f $CONFIG) { | |
save_tokens($app); | |
print "You should note these down but they have also been saved in $CONFIG\n\n"; | |
} else { | |
print "You should note these down or put them in $CONFIG with your consumer key and secret\n\n"; | |
} | |
get_user_prefs($app); | |
use YAML; | |
sub get_user_prefs { | |
my $prefs = $app->get_user_prefs; | |
warn Dump($prefs); | |
exit(0); | |
} | |
sub get_tokens { | |
my %tokens = FOSM->load_tokens($CONFIG); | |
while (@ARGV && $ARGV[0] =~ m!^(\w+)\=(\w+)$!) { | |
$tokens{$1} = $2; | |
shift @ARGV; | |
} | |
return %tokens; | |
} | |
sub save_tokens { | |
my $app = shift; | |
my %tokens = $app->tokens; | |
FOSM->save_tokens($CONFIG, %tokens); | |
} | |
package FOSM; | |
use strict; | |
use base qw(Net::OAuth::Simple); | |
my $root; | |
BEGIN | |
{ | |
$root="http://pine02.fosm.org/FOSM-Api/OSM-API-Proxy/public/dispatch.cgi/"; | |
} | |
sub new { | |
my $class = shift; | |
my %tokens = @_; | |
die unless $root; | |
return $class->SUPER::new( | |
tokens => \%tokens, | |
protocol_version => '1.0', | |
urls => { | |
authorization_url => $root . 'oauth/authorize', | |
request_token_url => $root . 'oauth/request_token', | |
access_token_url => $root . 'oauth/access_token', | |
# args["api"] = root + "api/0.6/"; | |
# args["policy"] = "http://pine02.fosm.org/api/crossdomain.xml"; | |
# args["connection"] = "XML"; | |
# args["oauth_policy"] = root + "oauth/crossdomain.xml"; | |
# args["oauth_request_url"] = root + "oauth/request_token"; | |
# args["oauth_access_url"] = root + "oauth/access_token"; | |
# args["oauth_auth_url"] = root + "oauth/authorize"; | |
# args["oauth_consumer_key"] = "yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ"; | |
# args["oauth_consumer_secret"] = "jJs8XFPjywSyrVlwkNvLanh7ZVyHaJWTQHWFOn1U"; | |
# args["serverName"] = "pine02.fosm.org"; | |
# args["show_help"] = "once"; | |
# args["site_name"] = "FOSM"; | |
# args["force_auth"] = "force"; | |
}); | |
} | |
sub get_user_prefs { | |
my $self = shift; | |
my $url = $root . "api/0.6/user/preferences"; | |
return $self->_make_restricted_request($url, 'GET'); | |
} | |
sub _make_restricted_request { | |
my $self = shift; | |
my $response = $self->make_restricted_request(@_); | |
return $response->content; | |
} | |
1; |
This file contains 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
#!perl -w | |
use strict; | |
use lib qw(lib); | |
# args["api"] = root + "api/0.6/"; | |
# args["policy"] = "http://www.fosm.org/api/crossdomain.xml"; | |
# args["connection"] = "XML"; | |
# args["oauth_policy"] = root + "oauth/crossdomain.xml"; | |
# args["oauth_request_url"] = root + "oauth/request_token"; | |
# args["oauth_access_url"] = root + "oauth/access_token"; | |
# args["oauth_auth_url"] = root + "oauth/authorize"; | |
# args["oauth_consumer_key"] = "yUV5Mk9FZYEGdFEfZ786AiMl6R9D0flYWXsu10bQ"; | |
# args["oauth_consumer_secret"] = "jJs8XFPjywSyrVlwkNvLanh7ZVyHaJWTQHWFOn1U"; | |
# args["serverName"] = "www.fosm.org"; | |
# args["show_help"] = "once"; | |
# args["site_name"] = "FOSM"; | |
# args["force_auth"] = "force"; | |
my $CONFIG = ".osmdev_config"; | |
binmode STDOUT, ":utf8"; | |
# Get the tokens from the command line, a config file or wherever | |
my %tokens = get_tokens(); | |
my $app = FOSM->new(%tokens); | |
# Check to see we have a consumer key and secret | |
unless ($app->consumer_key && $app->consumer_secret) { | |
die "You must go get a consumer key and secret from App\n"; | |
} | |
# If the app is authorized (i.e has an access token and secret) | |
# Then look at a restricted resourse | |
get_user_prefs($app) if $app->authorized; | |
# right, we need to get their access stuff | |
print "STEP 1: REQUEST OSM AUTHORIZATION FOR THIS APP\n"; | |
print "\tURL : ".$app->get_authorization_url( callback => 'oob' )."\n"; | |
print "\n-- Please go to the above URL and authorize the app"; | |
print "\n-- It will give you a code. Please type it here: "; | |
my $verifier = <STDIN>; print "\n"; | |
chomp($verifier); $verifier =~ s!(^\s*|\s*$)!!g; | |
$app->verifier($verifier); | |
my ($access_token, $access_token_secret) = $app->request_access_token(); | |
print "You have now authorized this app.\n"; | |
print "Your access token and secret are:\n\n"; | |
print "access_token=$access_token\n"; | |
print "access_token_secret=$access_token_secret\n"; | |
print "\n"; | |
if (-f $CONFIG) { | |
save_tokens($app); | |
print "You should note these down but they have also been saved in $CONFIG\n\n"; | |
} else { | |
print "You should note these down or put them in $CONFIG with your consumer key and secret\n\n"; | |
} | |
get_user_prefs($app); | |
use YAML; | |
sub get_user_prefs { | |
my $prefs = $app->get_user_prefs; | |
warn Dump($prefs); | |
exit(0); | |
} | |
sub get_tokens { | |
my %tokens = FOSM->load_tokens($CONFIG); | |
while (@ARGV && $ARGV[0] =~ m!^(\w+)\=(\w+)$!) { | |
$tokens{$1} = $2; | |
shift @ARGV; | |
} | |
return %tokens; | |
} | |
sub save_tokens { | |
my $app = shift; | |
my %tokens = $app->tokens; | |
FOSM->save_tokens($CONFIG, %tokens); | |
} | |
package FOSM; | |
use strict; | |
use base qw(Net::OAuth::Simple); | |
my $root; | |
BEGIN | |
{ | |
$root="http://api.fosm.org/api/0.6/"; | |
} | |
sub new { | |
my $class = shift; | |
my %tokens = @_; | |
die unless $root; | |
return $class->SUPER::new( | |
tokens => \%tokens, | |
protocol_version => '1.0', | |
urls => { | |
authorization_url => $root . 'oauth/authorize', | |
request_token_url => $root . 'oauth/request_token', | |
access_token_url => $root . 'oauth/access_token', | |
}); | |
} | |
sub get_user_prefs { | |
my $self = shift; | |
my $url = $root . "api/0.6/user/preferences"; | |
return $self->_make_restricted_request($url, 'GET'); | |
} | |
sub _make_restricted_request { | |
my $self = shift; | |
my $response = $self->make_restricted_request(@_); | |
return $response->content; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment