| 1 | package VKontakte::API::OAuth; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Digest::MD5 qw(md5 md5_hex); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use WWW::Mechanize; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =pod | ||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | VKontakte::API::OAuth - Module for login into vkontakte.ru using OAuth 2.0 and send requests | ||||||||||
| 16 | |||||||||||
| 17 | =head1 VERSION | ||||||||||
| 18 | |||||||||||
| 19 | Version 0.02 | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | 1 | our $VERSION = '0.02'; | |||||||||
| 24 | |||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||
| 26 | |||||||||||
| 27 | Register you application at http://vkontakte.ru/apps.php?act=add and get api_id and api_secret | ||||||||||
| 28 | Details: | ||||||||||
| 29 | http://vkontakte.ru/developers.php?o=-1&p=%C0%E2%F2%EE%F0%E8%E7%E0%F6%E8%FF%20%F1%E5%F0%E2%E5%F0%E0%20%EF%F0%E8%EB%EE%E6%E5%ED%E8%FF | ||||||||||
| 30 | |||||||||||
| 31 | use VKontakte::API::OAuth; | ||||||||||
| 32 | |||||||||||
| 33 | my $vk = VKontakte::API::OAuth->new( $opt->{api_id}, $opt->{api_secret} ); | ||||||||||
| 34 | my $h = $vk->sendRequest( "getProfiles", { uid => 66748 } ); | ||||||||||
| 35 | print Dumper($h); | ||||||||||
| 36 | |||||||||||
| 37 | =head1 SUBROUTINES/METHODS | ||||||||||
| 38 | |||||||||||
| 39 | =head2 new | ||||||||||
| 40 | |||||||||||
| 41 | Two parameters of registered application: | ||||||||||
| 42 | |||||||||||
| 43 | =over 4 | ||||||||||
| 44 | |||||||||||
| 45 | =item api_id | ||||||||||
| 46 | |||||||||||
| 47 | =item secret_key | ||||||||||
| 48 | |||||||||||
| 49 | =back | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 54 | 0 | my $class = shift; | |||||||||
| 55 | 0 | my $self = {}; | |||||||||
| 56 | 0 | bless( $self, $class ); | |||||||||
| 57 | |||||||||||
| 58 | 0 | $self->{api_id} = $_[0]; | |||||||||
| 59 | 0 | $self->{secret} = $_[1]; | |||||||||
| 60 | |||||||||||
| 61 | 0 | my $query='https://api.vkontakte.ru/oauth/access_token?client_id=' . $self->{api_id} . '&client_secret=' . $self->{secret}. '&grant_type=client_credentials'; | |||||||||
| 62 | |||||||||||
| 63 | 0 | my $mech = WWW::Mechanize->new( agent => 'VKontakte::API::OAuth' ); | |||||||||
| 64 | 0 | $mech->get($query); | |||||||||
| 65 | |||||||||||
| 66 | 0 | my $response = $mech->content(); | |||||||||
| 67 | 0 | utf8::encode($response); | |||||||||
| 68 | 0 | my $h=decode_json($response); | |||||||||
| 69 | 0 | ▶ | return undef unless(defined $h->{'access_token'}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 0 | $self->{'access_token'}=$h->{'access_token'}; | |||||||||
| 72 | 0 | return $self; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | =head2 sendRequest | ||||||||||
| 76 | |||||||||||
| 77 | $resp = $vk->sendRequest('getProfiles', {'uids'=>'123123'}); | ||||||||||
| 78 | |||||||||||
| 79 | =over 4 | ||||||||||
| 80 | |||||||||||
| 81 | =item method | ||||||||||
| 82 | |||||||||||
| 83 | Name of methods listed at http://vkontakte.ru/developers.php?o=-1&p=%D0%9E%D0%BF%D0%B8%D1%81%D0%B0%D0%BD%D0%B8%D0%B5+%D0%BC%D0%B5%D1%82%D0%BE%D0%B4%D0%BE%D0%B2+API | ||||||||||
| 84 | |||||||||||
| 85 | =item params | ||||||||||
| 86 | |||||||||||
| 87 | Parameters for method | ||||||||||
| 88 | |||||||||||
| 89 | =back | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub sendRequest { | ||||||||
|
Subroutine
sendRequest: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 94 | 0 | my $self = shift; | |||||||||
| 95 | 0 | my $method = $_[0]; | |||||||||
| 96 | 0 | my $params = $_[1]; | |||||||||
| 97 | |||||||||||
| 98 | 0 | my $query="https://api.vkontakte.ru/method/$method?".$self->_params($params)."&access_token=".$self->{'access_token'}; | |||||||||
| 99 | |||||||||||
| 100 | 0 | my $mech = WWW::Mechanize->new( agent => 'VKontakte::API::OAuth' ); | |||||||||
| 101 | 0 | my $r = $mech->get($query); | |||||||||
| 102 | |||||||||||
| 103 | 0 | my $response = $mech->content(); | |||||||||
| 104 | 0 | utf8::encode($response); | |||||||||
| 105 | 0 | return decode_json($response); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | sub _params { | ||||||||
|
Subroutine
_params: not called CC=3 SCAR=24.8
| |||||||||||
| 109 | 0 | my $self = shift; | |||||||||
| 110 | 0 | my $params = shift; | |||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | return unless ( ref $params eq "HASH" ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | |||||||||||
| 114 | 0 | my @pice; | |||||||||
| 115 | 0 | ▶ | while ( my ( $k, $v ) = each %$params ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 0 | push @pice, $k . '=' . $v;# _encurl($v); | |||||||||
| 117 | } | ||||||||||
| 118 | 0 | return join( '&', @pice ); | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | =head1 AUTHOR | ||||||||||
| 122 | |||||||||||
| 123 | Anastasiya Deeva, C<< <nastya at creograf.ru> >> | ||||||||||
| 124 | |||||||||||
| 125 | =head1 BUGS | ||||||||||
| 126 | |||||||||||
| 127 | Please report any bugs or feature requests to C<bug-vkontakte-api at rt.cpan.org>, or through | ||||||||||
| 128 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=VKontakte-API>. I will be notified, and then you'll | ||||||||||
| 129 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 130 | |||||||||||
| 131 | =head1 SUPPORT | ||||||||||
| 132 | |||||||||||
| 133 | You can find documentation for this module with the perldoc command. | ||||||||||
| 134 | |||||||||||
| 135 | perldoc VKontakte::API::OAuth | ||||||||||
| 136 | |||||||||||
| 137 | |||||||||||
| 138 | You can also look for information at: | ||||||||||
| 139 | |||||||||||
| 140 | =over 4 | ||||||||||
| 141 | |||||||||||
| 142 | =item * RT: CPAN's request tracker | ||||||||||
| 143 | |||||||||||
| 144 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=VKontakte-API> | ||||||||||
| 145 | |||||||||||
| 146 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 147 | |||||||||||
| 148 | L<http://annocpan.org/dist/VKontakte-API> | ||||||||||
| 149 | |||||||||||
| 150 | =item * CPAN Ratings | ||||||||||
| 151 | |||||||||||
| 152 | L<http://cpanratings.perl.org/d/VKontakte-API> | ||||||||||
| 153 | |||||||||||
| 154 | =item * Search CPAN | ||||||||||
| 155 | |||||||||||
| 156 | L<http://search.cpan.org/dist/VKontakte-API/> | ||||||||||
| 157 | |||||||||||
| 158 | =back | ||||||||||
| 159 | |||||||||||
| 160 | |||||||||||
| 161 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 162 | |||||||||||
| 163 | |||||||||||
| 164 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 165 | |||||||||||
| 166 | Copyright 2011 Anastasiya Deeva. | ||||||||||
| 167 | |||||||||||
| 168 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 169 | under the terms of either: the GNU General Public License as published | ||||||||||
| 170 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 171 | |||||||||||
| 172 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 173 | |||||||||||
| 174 | |||||||||||
| 175 | =cut | ||||||||||
| 176 | |||||||||||
| 177 | 1 | 1; | |||||||||