API-Medium
view release on metacpan or search on metacpan
2. You may apply bug fixes, portability fixes and other modifications derived
from the Public Domain or from the Copyright Holder. A Package modified in such
a way shall still be considered the Standard Version.
3. You may otherwise modify your copy of this Package in any way, provided that
you insert a prominent notice in each changed file stating how and when you
changed that file, and provided that you do at least ONE of the following:
a) place your modifications in the Public Domain or otherwise make them
Freely Available, such as by posting said modifications to Usenet or an
equivalent medium, or placing the modifications on a major archive site
such as ftp.uu.net, or by allowing the Copyright Holder to include your
modifications in the Standard Version of the Package.
b) use the modified Package only within your corporation or organization.
c) rename any non-standard executables so the names do not conflict with
standard executables, which must also be provided, and provide a separate
manual page for each non-standard executable that clearly documents how it
differs from the Standard Version.
# 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 );
# DESCRIPTION
It's probably a good idea to read [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
**not** documented here.
See `example/hello_medium.pl` for a complete script.
Create a new API client. You will need to pass in your `$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!
## get\_current\_user
my $data = $m->get_current_user;
Fetch the User "object".
You will need this to get the user `id` for posting. Depending on
your app you might want to store your `id` in some config file to
save one API call.
## publications
Not implemented yet. Listing the user's publications
/users/{{userId}}/publications
## contributors
Not implemented yet. Fetching contributors for a publication.
/publications/{{publicationId}}/contributors
## create\_post
my $url = $m->create_post( $user_id, $post_data );
Create a new post. If you pass in bad data, Medium will probably
report an error.
`publishStatus` is set to 'draft' unless you pass in another value.
## create\_publication\_post
my $url = $m->create_publication_post( $publication_id, $post_data );
Create a new post under a publication. You will need to figure out the
publication\_id by calling the API from the commandline (until
`publications` is implemented.)
If you pass in bad data, Medium will probably report an error.
`publishStatus` is set to 'draft' unless you pass in another value.
## TODO
- OAuth2 Login
- Get a new access\_token from refresh\_token
- `publications`
- `contributors`
## Thanks
Thanks to Dave Cross for starting [Cultured
Perl](https://medium.com/cultured-perl), which prompted me to write
this module so I can auto-post blogposts from [my private
blog](http://domm.plix.at) to medium.
# AUTHOR
Thomas Klausner <domm@plix.at>
# COPYRIGHT AND LICENSE
This software is copyright (c) 2016 - 2021 by Thomas Klausner.
example/hello_medium.pl view on Meta::CPAN
use API::Medium;
my $token = $ARGV[0];
print "Usage: $0 your_access_token\n" && exit unless $token;
my $m = API::Medium->new( { access_token => $token, } );
my $user = $m->get_current_user;
my $post_url = $m->create_post(
$user->{id},
{ "title" => "Hello, Medium",
"contentFormat" => "html",
"content" => "<h1>Hello, Medium</h1><p>It works...</p><p>s/ð/ð²/g</p>",
"tags" => [ "Perl", "CPAN", "API" ],
"publishStatus" => "draft",
#"canonicalUrl": "http://example.com/it-works.html",
}
);
print "Your post is ready: $post_url\n";
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) {
lib/API/Medium.pm view on Meta::CPAN
=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.
lib/API/Medium.pm view on Meta::CPAN
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;
Fetch the User "object".
You will need this to get the user C<id> for posting. Depending on
your app you might want to store your C<id> in some config file to
save one API call.
=head2 publications
Not implemented yet. Listing the user's publications
/users/{{userId}}/publications
=head2 contributors
Not implemented yet. Fetching contributors for a publication.
/publications/{{publicationId}}/contributors
=head2 create_post
my $url = $m->create_post( $user_id, $post_data );
Create a new post. If you pass in bad data, Medium will probably
report an error.
C<publishStatus> is set to 'draft' unless you pass in another value.
=head2 create_publication_post
my $url = $m->create_publication_post( $publication_id, $post_data );
Create a new post under a publication. You will need to figure out the
publication_id by calling the API from the commandline (until
C<publications> is implemented.)
If you pass in bad data, Medium will probably report an error.
C<publishStatus> is set to 'draft' unless you pass in another value.
=head2 TODO
=over
lib/API/Medium.pm view on Meta::CPAN
=item * C<publications>
=item * C<contributors>
=back
=head2 Thanks
Thanks to Dave Cross for starting L<Cultured
Perl|https://medium.com/cultured-perl>, which prompted me to write
this module so I can auto-post blogposts from L<my private
blog|http://domm.plix.at> to medium.
=head1 AUTHOR
Thomas Klausner <domm@plix.at>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 - 2021 by Thomas Klausner.
( run in 0.837 second using v1.01-cache-2.11-cpan-ceb78f64989 )