Upload post to blogger with Perl/Python (OAuth 2.0 authorozation)
#!/usr/bin/perl | |
# *** Wyslanie posta na blogger.com *** | |
use strict; | |
use LWP::UserAgent; | |
use XML::LibXML; | |
use Getopt::Long; | |
my $profileID="default"; | |
my $blogID = '1928418645181504144'; # Identyfikator bloga | |
my $blog_entry ; | |
## Na wypadek gdy ktoś ma kilka blogów moża podać na któr | |
## ma być wysłany post używając opcji -blog | |
GetOptions( "blog=s" => \$blogID, "post=s" => \$blog_entry) ; | |
if ( $blog_entry eq '' ) { | |
print STDERR "*** USAGE: $0 -b blog -p message (-b is optional) ***\n" } | |
## sprawdź czy post jest well formed: | |
my $parser = XML::LibXML->new(); | |
eval {my $res_ = $parser->parse_string($blog_entry) }; | |
if ($@) { die "*** Error parsing post message! \n"; } | |
my $ACCESS_TOKEN=`oauth2blogger.py`; # ACCESS_TOKEN via python script see https://gist.github.com/hrpunio/da9d1a7c49a07a27d874 | |
print STDERR "*** AccessToken: $ACCESS_TOKEN ***\n"; | |
my $req = HTTP::Request->new( | |
POST => "https://www.blogger.com/feeds/$blogID/posts/default"); | |
$req->header( 'Content-Type' => 'application/atom+xml' ); | |
$req->header( 'Authorization' => "Bearer $ACCESS_TOKEN" ); | |
$req->header( 'GData-Version' => '2' ); | |
$req->content($blog_entry); | |
my $ua = LWP::UserAgent->new; | |
my $res = $ua->request($req); | |
# Jeżeli coś jest nie tak poniższe drukuje verbatim: | |
# http://www.perlmonks.org/bare/?node_id=464442 | |
# $ua->prepare_request($req); print($req->as_string); exit ; | |
if ($res->is_success) { | |
my $decoded_response = $res->decoded_content; | |
print STDERR "*** OK *** $decoded_response\n"; } | |
else { die $res->status_line; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment