Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How to access github with Pithub/Net::GitHub::V3?

by no longer just digit (Beadle)
on Feb 22, 2021 at 00:23 UTC ( [id://11128640]=perlquestion: print w/replies, xml ) Need Help??

no longer just digit has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to access the github API from Perl.

I tried two modules, Pithub and Net::GitHub::V3. In both cases I could access public data, but the only response I got was endless "Not Found" or "Authentication Required" when I tried to authenticate. In the case of Net::GitHub::V3, I tried both the token and the login/pass methods of logging in, but all I got was "Authentication Required".

The NGV script looks like this:

use Net::GitHub::V3; my $gh = Net::GitHub::V3->new ( login => '####', pass => '%%%%', ); my $repos = $gh->repos; my $rp = $gh->repos->create ({ name => 'hello-world', description => 'monkey business', homepage => 'www.exmpale.org', });
The documentation doesn't make it clear what else is required. For Pithub, I tried to use the token method, but that also failed.
use FindBin '$Bin'; use Pithub; use Data::Dumper; use File::Slurper 'read_text'; my $token = read_text ("$Bin/token"); $token =~ s/\s//g; my $pid = Pithub->new ( user => '####', repo => '%%%%', token => $token, ); my $result = $pid->repos->get (); print Dumper $result;

Does anyone have a short working example of accessing github using either of these, or alternatively is there another module which has a documented example of how to log in?

Update I found App::ph on CPAN, but this is failing with the "Not Found" error when I type in my name and password.

$ ph setup ph. please input your id/pw to this prompt. password will not sa +ve to any location. user: %%%% pass: #### Use of uninitialized value in pattern match (m//) at /home/ben/softwar +e/install/bin/ph line 362. HTTP/1.1 404 Not Found Connection: close Date: Mon, 22 Feb 2021 01:32:35 GMT Server: GitHub.com Vary: Accept-Encoding, Accept, X-Requested-With Content-Length: 74 Content-Type: application/json; charset=utf-8 Access-Control-Allow-Origin: * Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-Gi +tHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, + X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-I +nterval, X-GitHub-Media-Type, Deprecation, Sunset Client-Date: Mon, 22 Feb 2021 01:32:35 GMT Client-Peer: 52.69.239.207:443 Client-Response-Num: 1 Client-SSL-Cert-Issuer: /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=Di +giCert SHA2 High Assurance Server CA Client-SSL-Cert-Subject: /C=US/ST=California/L=San Francisco/O=GitHub, + Inc./CN=*.github.com Client-SSL-Cipher: TLS_AES_128_GCM_SHA256 Client-SSL-Socket-Class: IO::Socket::SSL Client-SSL-Version: TLSv1_3 Content-Security-Policy: default-src 'none' Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-or +igin Strict-Transport-Security: max-age=31536000; includeSubdomains; preloa +d X-Content-Type-Options: nosniff X-Frame-Options: deny X-GitHub-Media-Type: github.v3; format=json X-GitHub-Request-Id: EE38:0A8D:5C048:694DB:603309B3 X-RateLimit-Limit: 60 X-RateLimit-Remaining: 59 X-RateLimit-Reset: 1613961155 X-Ratelimit-Used: 1 X-XSS-Protection: 1; mode=block {"message":"Not Found","documentation_url":"https://docs.github.com/re +st"} $
This endless "Not Found" is rather frustrating.

Replies are listed 'Best First'.
Re: How to access github with Pithub/Net::GitHub::V3?
by tobyink (Canon) on Feb 22, 2021 at 10:16 UTC

    This works for me and shows all my repos, including private repos.

    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; use Pithub; use constant GH_USER => 'tobyink'; # Your username! use constant GH_TOKEN => 'ABCX123'; # https://github.com/settings/toke +ns my $gh = 'Pithub'->new( user => GH_USER, token => GH_TOKEN, ); my $repos = $gh->repos->list; $repos->auto_pagination( 1 ); while ( my $repo = $repos->next ) { say $repo->{'name'}; }
      I've been able to access passive information but every attempt to change something using the API gets these "Not Found" errors.
        Have you configured the "Scopes" of the token to allow that kind of access?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        Are you using the latest release of Pithub? Github do occasionally move bits of their API around — not too often, but if it's a very old Pithub, that could be an issue.

Re: How to access github with Pithub/Net::GitHub::V3?
by Discipulus (Canon) on Feb 22, 2021 at 07:21 UTC
    Hello no longer just digit

    not really the answer to your question but you can be interested to Back up all of your Github repos which uses Git::Repository and this module list other perl git wrappers

    Loooking at Net::GitHub::V3 module seems nice and has some example too. The lacks of public tests is no good, but there are author tests under /xt/v3: I suggest you to look at them, set all needed ENV vars an run them to see if they all pass for you.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      not really the answer to your question but you can be interested to Back up all of your Github repos which uses Git::Repository and this module list other perl git wrappers

      Thanks. Of those suggestions, I am using Git::Raw which I think is OK and it is basically well documented. I wrote a blog post about its documentation here, it would be much easier if they would put the functions into some kind of order.

      Loooking at Net::GitHub::V3 module seems nice and has some example too. The lacks of public tests is no good, but there are author tests under /xt/v3: I suggest you to look at them, set all needed ENV vars an run them to see if they all pass for you.

      Yes that seems like a good idea, I will try it and reply back. The documentation of that module seems to be not too bad, but I couldn't work out how to debug what was happening with the error message. I think I need to download the source code and run a script after setting %ENV up.

      OK I thought I should do that before posting this. The good news is that I could get 01-access-token.t to pass after setting my token in %ENV. The bad news is that the rest of it didn't work. This blog post says that authentication changed in November 2020. Maybe I can work things out with the token though.

      I tried my previous script as shown at the top using the access_token method, and we are back to the Not Found again. I think there is something wrong here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11128640]
Approved by jcb
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-24 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found