App-Nopaste

 view release on metacpan or  search on metacpan

lib/App/Nopaste/Service/Gist.pm  view on Meta::CPAN

    my ($self) = @_;

    if (my $oauth_token = $ENV{GITHUB_OAUTH_TOKEN}) {
        return (oauth_token => $oauth_token);
    }
    elsif ($ENV{GITHUB_USER} && $ENV{GITHUB_PASSWORD}) {
        return (
            username => $ENV{GITHUB_USER},
            password => $ENV{GITHUB_PASSWORD},
        );
    }
    else {
        # this is also done by the fallback mechanism in Config::Identity::GitHub.
        my $github_config = path('~', '.github');
        if (-f $github_config) {
            my $content = $github_config->slurp_utf8;
            my ($username) = $content =~ m/\blogin (.+)(?:$|#)/m;
            my ($password) = $content =~ m/\bpassword (.+)(?:$|#)/m;
            return (
                username => $username,
                password => $password,
            ) if $username and $password;
        }
    }

    die join("\n",
        "Export GITHUB_OAUTH_TOKEN first. For example:",
        "    perl -MApp::Nopaste::Service::Gist -e 'App::Nopaste::Service::Gist->create_token'",
        "",
        "OR you can export GITHUB_USER and GITHUB_PASSWORD.",
        "OR you can set 'login' and 'password' in ~/.github.",
    ) . "\n";
}

sub create_token {
    my ($self) = @_;

    local $| = 1;
    print "Username: ";
    chomp(my $username = <>);
    print "Password: ";
    chomp(my $password = <>);
    print "\n\n";

    exit unless $username && $password;

    my $parameters = {
        scopes   => ["gist"],
        note     => "App::Nopaste",
        note_url => "https://metacpan.org/module/App::Nopaste",
    };

    my $ua = LWP::UserAgent->new;

    my $request = HTTP::Request->new(POST => 'https://api.github.com/authorizations');
    $request->authorization_basic($username, $password);
    $request->content(encode_json($parameters));

    my $response = $ua->request($request);

    my $response_content = decode_json($response->decoded_content);

    if ($response_content->{token} ) {
        print "GITHUB_OAUTH_TOKEN=$response_content->{token}\n";
    }
    else {
        print $response_content->{message} || "Unspecified error", "\n";
    }
}

sub return {
    my ($self, $res) = @_;

    if ($res->is_error) {
        my $text = $res->status_line;
        if ($res->code == 401) {
            $text .= "\nYou may need to authorize $0. See `perldoc " . __PACKAGE__ . "`";
        }
        return (0, "Failed: " . $text);
    }

    if (($res->header('Client-Warning') || '') eq 'Internal response') {
      return (0, "LWP Error: " . $res->content);
    }

    my $id = decode_json($res->content)->{id};

    return (0, "Could not find paste link.") if !$id;
    return (1, "https://gist.github.com/$id");
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Nopaste::Service::Gist - Service provider for GitHub gist - http://gist.github.com/

=head1 VERSION

version 1.013

=for stopwords SIGNES gists oauth plaintext

=head1 GitHub Authorization

In order to create gists you have to get an oauth token. That could be easily
obtained via curl:

    curl -X POST 'https://USERNAME:PASSWORD@api.github.com/authorizations' \
        -d '{"scopes":["gist"],"note":"App::Nopaste"}'

or you can use this module to do the same:

    perl -MApp::Nopaste::Service::Gist -e 'App::Nopaste::Service::Gist->create_token'



( run in 0.883 second using v1.01-cache-2.11-cpan-524268b4103 )