API-Medium

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

    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.

## Authentication

### OAuth2 Login

Not implemented yet, mostly because medium only support the "web
server" flow and I'm using `API::Medium` for an installed

README.md  view on Meta::CPAN

    my $m = API::Medium->new({
         access_token => $token,
    });

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

README.md  view on Meta::CPAN

    /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`

lib/API/Medium.pm  view on Meta::CPAN

            'Content-Type'  => 'application/json',
        }
    );
}

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} );

lib/API/Medium.pm  view on Meta::CPAN

  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

lib/API/Medium.pm  view on Meta::CPAN

  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;

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

lib/API/Medium.pm  view on Meta::CPAN

  /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

=item * OAuth2 Login

=item * Get a new access_token from refresh_token



( run in 0.384 second using v1.01-cache-2.11-cpan-496ff517765 )