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.
I believe AUTH_URL_CLIENT_ID
should be Github\Client::AUTH_URL_CLIENT_ID
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!