API-Medium
view release on metacpan or search on metacpan
lib/API/Medium.pm view on Meta::CPAN
}
sub get_current_user {
my $self = shift;
my $res = $self->_request( 'GET', 'me' );
return $res->{data};
}
sub create_post {
my ( $self, $user_id, $post ) = @_;
$post->{publishStatus} ||= 'draft';
my $res = $self->_request( 'POST', 'users/' . $user_id . '/posts', $post );
return $res->{data}{url};
}
sub create_publication_post {
my ( $self, $publication_id, $post ) = @_;
$post->{publishStatus} ||= 'draft';
my $res =
$self->_request( 'POST', 'publications/' . $publication_id . '/posts',
$post );
return $res->{data}{url};
}
sub _request {
my ( $self, $method, $endpoint, $data ) = @_;
my $url = join( '/', $self->server, $endpoint );
my $res;
if ($data) {
$res = $self->_client->request( $method, $url,
{ content => encode_json($data) } );
}
else {
$res = $self->_client->request( $method, $url );
}
if ( $res->{success} ) {
return decode_json( $res->{content} );
}
else {
$log->errorf( "Could not talk to medium: %i %s",
$res->{status}, $res->{reason} );
die join( ' ', $res->{status}, $res->{reason} );
}
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
API::Medium - Talk with medium.com using their REST API
=head1 VERSION
version 0.902
=head1 SYNOPSIS
use API::Medium;
my $m = new({
access_token=>'your_token',
});
my $hash = $m->get_current_user;
say $hash->{id};
my $url = $m->create_post( $user_id, $post );
my $other_url = $m->create_publication_post( $publication_id, $post );
=head1 DESCRIPTION
It's probably a good idea to read L<the Medium API
docs|https://github.com/Medium/medium-api-docs> first, especially as
the various data structures you have to send (or might get back) are
B<not> documented here.
See F<example/hello_medium.pl> for a complete script.
=head2 Authentication
=head3 OAuth2 Login
Not implemented yet, mostly because medium only support the "web
server" flow and I'm using C<API::Medium> for an installed
application.
=head3 Self-issued access token / Integration token
Go to your L<settings|https://medium.com/me/settings>, scroll down to
"Integration tokens", and either create a new one, or pick the one you
want to use.
=head1 Methods
=head2 new
my $m = API::Medium->new({
access_token => $token,
});
Create a new API client. You will need to pass in your C<$token>, see
above on how to get it. Please make sure no not leak your Integration
Token. If you do, anybody who has it can take over your Medium page!
=head2 get_current_user
my $data = $m->get_current_user;
( run in 1.428 second using v1.01-cache-2.11-cpan-bbb979687b5 )