3

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.

1
  • This is a shameless plug, but it will help you see what the client is sending back and forth using Runscope. Create a free account, note your bucket key, then add this line after you create the client: $client.setOption("base_url", "api-github-com-yourbucketkey.runscope.net");. Then make the request again and view in your Runscope account. Then you can see the actual HTTP request/response generated and the error Github is returning. Commented Aug 2, 2013 at 19:22

3 Answers 3

1

I believe AUTH_URL_CLIENT_ID should be Github\Client::AUTH_URL_CLIENT_ID

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.