blib/lib/Net/Social/Service/Facebook.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Net::Social::Service::Facebook;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw(Net::Social::Service);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use WWW::Facebook::API;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Net::Social qw(:all);
8 use vars qw($VERSION);
9  
10  
11 $VERSION = "0.1";
12  
13 =head1 NAME
14  
15 Net::Social::Service::Facebook - a Facebook plugin for Net::Social
16  
17 =head1 PARAMS
18  
19 For reading Net::Social::Service::Facebook needs
20  
21 =over4
22  
23 =item api_key
24  
25 You need an API key from Facebook. You can read how to
26 get one from here
27  
28 http://developers.facebook.com/get_started.php
29  
30 =item session_secret
31  
32 See below.
33  
34 =item session_key
35  
36 See below.
37  
38 =back
39  
40 =cut
41  
42 sub params {(
43 read => {
44 "api_key" => { required => 1,
45 description => "Your Facebook API key",
46 },
47 "session_secret" => { required => 1,
48 description => "Your Facebpook API secret",
49 },
50 "session_key" => { required => 1,
51 description => "Your Facebpook API secret",
52 },
53 },
54 )}
55  
56 =head1 GETTING A PERMANENT FACEBOOK SESSION
57  
58 I use this script
59  
60 #!perl -w
61  
62 use strict;
63 use WWW::Facebook::API;
64  
65 my $api_key = shift || die "You must pass an api key\n"; # get these when you sign up
66 my $secret = shift || die "You must pass an api secret"; # as a facebook developer
67 my $client = make_client($api_key, $secret);
68  
69 # Now get the auth token
70 print "1) Go to ".$client->get_infinite_session_url."\n";
71 print "2) Press 'Generate' and get token\n";
72 print "3) Enter the token now\n";
73 chomp(my $token = <>);
74  
75 # Get a session
76 $client->auth->get_session($token);
77 $session_key = $client->session_key;
78 $session_secret = $client->secret;
79  
80 # And note the session key
81 print "4) Your infinite session key is $session_key\n";
82 print "5) Your infinite session secret is $session_secret\n";
83  
84 # Now make a new client and set the session key again to see that it works
85 my $new_client = make_client($api_key, $session_secret);
86 $new_client->session_key($session_key);
87  
88 # And get my friends
89 my $friends = $new_client->users->get_info( uids => $new_client->friends->get, fields => 'name');
90 print join("\n", map { $_->{name} } @$friends)."\n";
91  
92 sub make_client {
93 my ($api_key, $secret) = @_;
94  
95 my $client = WWW::Facebook::API->new(
96 parse => 1,
97 throw_errors => 1,
98 );
99 $client->api_key($api_key);
100 $client->secret($secret);
101 $client->desktop(1);
102 return $client;
103 }
104  
105 =head1 METHODS
106  
107 =head2 friends
108  
109 Returns your friends. It defines the keys C<uid>, C<name> and C<type>.
110  
111 =cut
112  
113 sub friends {
114 my $self = shift;
115 return () unless $self->{_logged_in};
116 my $user = $self->{_details}->{username};
117 my $key = $self->{_details}->{api_key};
118 my $sess_key = $self->{_details}->{session_key};
119 my $sess_secret = $self->{_details}->{session_secret};
120 my $client = WWW::Facebook::API->new(
121 desktop => 1,
122 parse => 1,
123 throw_errors => 1,
124 # debug => 1,
125 );
126 #$secret = $sess_secret if defined $sess_secret.
127 $client->api_key($key);
128 $client->session_key($sess_key);
129 $client->secret($sess_secret);
130 $client->desktop(1);
131  
132 my %friends;
133 my $uids = $client->friends->get;
134 # First get their details
135 foreach my $friend (@{$client->users->get_info(uids=>$uids,
136 fields=>[qw(name uid )])
137 })
138 {
139 $friends{$friend->{uid}} = { id => $friend->{uid}, name => $friend->{name}, type => MUTUAL };
140  
141 }
142 return values %friends;
143 }
144  
145 =head1 AUTHOR
146  
147 Simon Wistow <simon@thegestalt.org>
148  
149 =head1 COPYRIGHT
150  
151 Copyright, 2007 - Simon Wistow
152  
153 Distributed under the same terms as Perl itself
154  
155 =cut
156  
157  
158 1;
↑ Summary