Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Authentication using php-github-api

I am trying to implement Github authentication in my application (built using Laravel 4) using php-github-api by KnpLabs. I went ahead and created an app in my Github account to obtain the client_id and the secret key.

The problem is, I cannot authenticate using this library. A null result is returned. It implementation seems simple but I cannot get it work. Checkout how I am implementing it.

try{
        $client = new Github\Client();

        $auth= $client->authenticate('myclientid','mysecret',AUTH_URL_CLIENT_ID);

        $emails = $client->api('current_user')->emails()->all();

        return Response::json(array("user"=>$emails));
    }catch(Exception $e){
        return Response::json(array('failed',$e->getMessage()));
    }

This is the result I get from the above:

 ["failed","Requires authentication"]

Please someone help me figure out what I am doing wrong.

Thank you.

like image 481
felix cheruiyot Avatar asked May 28 '26 01:05

felix cheruiyot


2 Answers

I believe AUTH_URL_CLIENT_ID should be Github\Client::AUTH_URL_CLIENT_ID

like image 125
sdmeyers Avatar answered May 30 '26 16:05

sdmeyers


Update, Github\Client::AUTH_URL_CLIENT_ID no longer used. Please see:

https://github.com/KnpLabs/php-github-api/blob/v3.3.0/doc/security.md

Supported methods are:

  • Github\Client::AUTH_CLIENT_ID
  • Github\Client::AUTH_ACCESS_TOKEN
  • Github\Client::AUTH_JWT

In this case $client->authenticate('myclientid','mysecret',AUTH_URL_CLIENT_ID) should be:

$client->authenticate('myclientid', 'mysecret', Github\Client::AUTH_CLIENT_ID)

If using auth token, we can ommit the mysecret/password part:

$client->authenticate('myauthtoken', Github\Client::AUTH_ACCESS_TOKEN);
like image 42
Dale Ryan Avatar answered May 30 '26 16:05

Dale Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!