felix cheruiyot
felix cheruiyot

Reputation: 311

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.

Upvotes: 3

Views: 2529

Answers (3)

Dale Ryan
Dale Ryan

Reputation: 843

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

Upvotes: 1

mbontemps
mbontemps

Reputation: 99

This is more of a Github matter rather than a php-github-api question, really:

curl "https://api.github.com/user/emails?client_id=xxxx&client_secret=yyyy"

will fail too, whereas:

curl "https://api.github.com/user/emails" -u mbontemps

works just fine.

I think the reason is that client_id and client_secret authenticate an application, while the second command authenticates a user.

It's like you're asking: "hey, application, tell me your emails".
And the app is answering: "wait, what? I'm no user, dude!"

So IMO you'll need to authorize the application for your user (which will give you a token) and then use this token to authenticate with the OAuth mechanism.

cf. http://developer.github.com/v3/#basic-authentication

Good luck!

Upvotes: 0

sdmeyers
sdmeyers

Reputation: 259

I believe AUTH_URL_CLIENT_ID should be Github\Client::AUTH_URL_CLIENT_ID

Upvotes: 1

Related Questions