/examples/direct_spore_oauth.pl
Perl | 149 lines | 86 code | 25 blank | 38 comment | 8 complexity | 8c9028efa909e34bc7201b00103d8d8c MD5 | raw file
- #!/usr/bin/perl -w
- #
- use strict;
- use warnings;
- use feature ('say');
- use Net::HTTP::Spore;
- use Try::Tiny;
- use Data::Dumper;
- my $USER = 'Mekk';
- sub display_repos_reply {
- my $repos = shift;
- # print Dumper($repos);
- say "next:", $repos->{next} if $repos->{next};
- say "size:", $repos->{size};
- say "page:", $repos->{page};
- foreach my $val (@{$repos->{values}}) {
- say "Repo ", $val->{name}, "(scm: $val->{scm}, language: $val->{language}, owner: $val->{owner}->{username}, size: $val->{size}, private: $val->{is_private})";
- # say Dumper($val->{owner}); username, uuid, display_name, links
- # description
- # has_wiki, has_issues, fork_policy
- # updated_on, created_on
- say " full_name: ", $val->{full_name};
- say " fork_parent: ", $val->{parent} if $val->{parent};
- print " clone: "; #, Dumper($val->{links}->{clone});
- foreach my $link (@{$val->{links}->{clone}}) {
- print "[$link->{name}] $link->{href} ";
- }
- print "\n";
- }
- }
- try {
- my $client = Net::HTTP::Spore->new_from_spec(
- '../share/bitbucket-spore.json',
- # trace=>1, # Or SPORE_TRACE=1 in environment
- );
- # $client->enable()
- $client->enable('Format::JSON');
- $client->enable('Redirection', max_redirect => 3);
- # https://confluence.atlassian.com/display/BITBUCKET/OAuth+on+Bitbucket
- # https://confluence.atlassian.com/display/BITBUCKET/OAuth+consumer+examples
- # http://requests-oauthlib.readthedocs.org/en/latest/examples/bitbucket.html
- # Get from web interface (<me> -> Manage Account -> Integrated Applications or OAuth -> Add consumer )
- my $consumer_key = '';
- my $consumer_secret = '';
- # (both can be also obtained using oauth resource of users endpoint 1.0)
- # To be fetched dynamically (via api)
- # POST request to https://bitbucket.org/api/1.0/oauth/request_token.
- # Your POST must include (all required):
- # - oauth_consumer_key (from above)
- # - oauth_nonce A random string, uniquely generated for each request. The nonce allows the Service Provider to verify that a request has never been made before and helps prevent replay attacks when requests are made over a non-secure channel (such as HTTP).
- # - oauth_signature The signature as defined by the consumer. OAuth does not mandate a particular signature method, as each implementation can have its own unique requirements. Currently, Bitbucket only supports HMAC-SHA1 or PLAINTEXT signatures.
- # - oauth_signature_method The signature method the consumer used to sign the request. This is determined by your application.
- # - oauth_timestamp The number of seconds since January 1, 1970 00:00:00 GMT. The timestamp value MUST be a positive integer and MUST be equal or greater than the timestamp used in previous requests. If the timestamp is not within a few minutes either side of the actual current time, the request may be rejected.
- # oauth_callback The URL to redirect a user to should they approve your application's access to their account. For example: http%3A%2F%coolapp.local%2Fauth.php,bitbucketclient%3A%2F%2Fcallback
- #
- # Bitbucket replies with POST body containing:
- # - oauth_token_secret The token shared-secret.
- # - oauth_token The request token.
- # - oauth_callback_confirmed Must be present and true.
- my $token = '';
- my $token_secret = '';
- # Redirect user to authorize
- # To get authorization, your webserver makes a GET to https://bitbucket.org/api/1.0/oauth/authenticate and passes the oauth_token (request token) returned in the last step's POST. For example, the following call:
- # https://bitbucket.org/api/1.0/oauth/authenticate?oauth_token=4vyW6b49ZxcZDK64eY
- # If the user isn't logged into Bitbucket, Bitbucket asks the user to login. In this way, your application verifies the identity of the account holder. Then, Bitbucket confirms with the user that your application should have account access:
- #
- # Bitbucket returns in the redirect an oauth_verifier along with the oauth_token your application provided in its request.
- #
- # http://localhost/?dump&oauth_verifier=2287965216&oauth_token=URjPjc8CxLgNBdh7zL
- # Your application should store the oauth_verifier so you can use it in the next step. Bitbucket requires use of the oauth_verifier which is an OAuth 1.0a feature.
- $client->enable(
- 'Auth::OAuth',
- consumer_key => $consumer_key,
- consumer_secret => $consumer_secret,
- token => $token,
- token_secret => $token_secret,
- );
- $client->enable(sub {
- my $self = shift;
- my $request = shift;
- # say "Issuing request ", ref($request);
- say "* Issuing request to: ", $request->uri;
- $self->response_cb(sub {
- my $res = shift;
- say "* Obtained response, status: ", $res->status, ", content type: ", $res->content_type, ", size: ", $res->content_length;
- });
- });
- say "*** USER INFO ***";
- my $result;
- $result = $client->user();
- my $user = $result->body;
- say Dumper($user);
- say "*** OWNED REPOSITORIES ***";
- $result = $client->repositories();
- my $repos = $result->body;
- display_repos_reply($repos);
- $result = $client->repositories(page=>"2");
- $repos = $result->body;
- display_repos_reply($repos);
- while( $repos->{next} ) {
- if($repos->{next} =~ /page=(\d+)/) {
- last if $1 > 10;
- $result = $client->repositories(page=>$1);
- $repos = $result->body;
- display_repos_reply($repos);
- } else {
- last;
- }
- }
- }
- catch {
- say "Unexpected error caught [" . ref($_) . "]: $_";
- if(ref($_) eq 'Net::HTTP::Spore::Response') {
- my $body = $_->body;
- my $error = $body;
- if(ref($error)) {
- $error = $error->{error};
- }
- if(ref($error)) {
- $error = $error->{message};
- }
- say $error;
- }
- };