Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[ljcom] r10877: LJSV-1715 (Entries failing to crosspost ...

Committer: ailyin
LJSV-1715 (Entries failing to crosspost to Facebook)
A   trunk/cgi-bin/LJ/Console/Command/CheckToken.pm
Added: trunk/cgi-bin/LJ/Console/Command/CheckToken.pm
===================================================================
--- trunk/cgi-bin/LJ/Console/Command/CheckToken.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Console/Command/CheckToken.pm	2011-08-17 09:33:10 UTC (rev 10877)
@@ -0,0 +1,149 @@
+package LJ::Console::Command::CheckToken;
+use strict;
+use warnings;
+
+use base qw( LJ::Console::Command );
+
+use Data::Dumper qw();
+use Net::OAuth;
+
+use LJ::Client::Twitter;
+use LJ::JSON;
+
+sub cmd {
+    return 'check_token';
+}
+
+sub desc {
+    return
+        'Check whether the access token of the user is valid for ' .
+        'accessing a third-party site';
+}
+
+sub usage {
+    return '<type> <user>';
+}
+
+sub args_desc {
+    return [
+        'type' => 'Type of the token (facebook or twitter)',
+        'user' => 'The user to check',
+    ];
+}
+
+sub can_execute {
+    my $remote = LJ::get_remote();
+    return LJ::check_priv( $remote, 'supporthelp' );
+}
+
+sub execute {
+    my ( $self, $type, $username, @args_remainder ) = @_;
+
+    return $self->error('Too few arguments')
+        unless defined $type && defined $username;
+
+    return $self->error('Too many arguments') if @args_remainder;
+
+    my $u = LJ::load_user($username);
+    unless ($u) {
+        return $self->error("$username: no such user");
+    }
+
+    $username = $u->username;
+
+    if ( $type eq 'facebook' ) {
+        unless ($LJ::FACEBOOK_CONNECT_API_KEY) {
+            return $self->error("$type is disabled on this server");
+        }
+
+        my $token = $u->prop('facebook_access_token');
+
+        unless ($token) {
+            $self->info("$username: doesn't have a $type token");
+            return 1;
+        }
+
+        my $ua = LJ::get_useragent( 'role' => 'facebook_auth' );
+        my $res = $ua->get(
+            'https://graph.facebook.com/me?' .
+            'access_token=' . LJ::eurl($token)
+        );
+
+        $self->info(
+            "$username: request sent and returned HTTP " . $res->status_line );
+
+        my $data = LJ::JSON->from_json( $res->content );
+
+        if ( $data->{'error'} ) {
+            $self->info( "Protocol error: " . $data->{'error'}->{'message'} );
+            return 1;
+        }
+
+        $self->info("Everything seems ok");
+        return 1;
+    }
+
+    if ( $type eq 'twitter' ) {
+        unless ($LJ::TWITTER_CONSUMER_KEY) {
+            return $self->error("$type is disabled on this server");
+        }
+
+        my $token = $u->prop('twitter_access_token');
+
+        unless ($token) {
+            $self->info("$username: doesn't have a $type token");
+            return 1;
+        }
+
+        my $token_secret = $u->prop('twitter_access_token_secret');
+
+        unless ($token_secret) {
+            $self->info(
+                "$username: has a token but not a secret, data corrupt?");
+
+            return 1;
+        }
+
+        # we're constructing a request manually here so that we can
+        # be more verbose
+        my %params = LJ::Client::Twitter->default_request_params;
+        $params{'request_method'} = 'GET';
+
+        my $request_url
+            = 'https://api.twitter.com/1/account/verify_credentials.json';
+
+        my $request = Net::OAuth::ProtectedResourceRequest->new(
+            %params,
+            'token'           => $token,
+            'token_secret'    => $token_secret,
+            'request_url'     => $request_url,
+            'extra_params'    => {},
+        );
+
+        $request->sign;
+
+        my $ua = LJ::get_useragent(
+            'role' => 'twitter_auth',
+            'timeout' => $LJ::TWITTER_API_TIMEOUT,
+        );
+
+        my $requrl = $request->to_url;
+        my $res = $ua->get($requrl);
+
+        $self->info(
+            "$username: request sent and returned HTTP " . $res->status_line );
+
+        my $data = LJ::JSON->from_json( $res->content );
+        if ( $data->{'error'} ) {
+            $self->info( "Protocol error: " . $data->{'error'} );
+            return 1;
+        }
+
+        $self->info("Everything seems ok");
+        return 1;
+    }
+
+    return $self->error("Invalid type $type");
+}
+
+1;

Tags: andy, ljcom, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments