David Jones
David Jones

Reputation: 4305

PHP-GitHub-Api Authentication Issue

I am trying to authenticate a user using the php-github-api library. So far I have sent the user to Github to allow my application access and I successfully get a token back. I'm not sure what to do now. Here is my code.

The URL I send the user to Github with.

https://github.com/login/oauth/authorize?scope=repo,user&client_id=<client_id>

Then with the php-github-api I am doing this. The $token variable is the code that is sent in the $_GET array when the user is redirected to the callback.

        $client = new \Github\Client();
        try {
            $auth = $client->authenticate($token, Github\Client::AUTH_HTTP_TOKEN);
        } catch (Exception $e) {
            dp($e);
        }

Does anyone know if this is the correct method to authenticate a user? When I try and call a method the requires an authenicated user I get a 401 status code and an error in return.

Thanks in advance!

Upvotes: 19

Views: 1211

Answers (2)

David Jones
David Jones

Reputation: 4305

Thanks everyone for their suggestions. Seems like you have to feed the access_token into the authenticate method so an easy fix I implemented was a CURL request to grab the access_token then adding it to the authenticate method in the callback.

        $token  = $_POST['token'];
        $params = [
            'client_id'     => self::$_clientID,
            'client_secret' => self::$_clientSecret,
            'redirect_uri'  => 'url goes here',
            'code'          => $token,
        ];

    try {
        $ch = curl_init('https://github.com/login/oauth/access_token');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        $headers[] = 'Accept: application/json';

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($ch);
    } catch (\Exception $e) {
        dp($e->getMessage());
    }

Then in the call back we can call the authenticate method to and cache it somewhere, currently I am doing this in the session.

$client = self::getClient();
    $_SESSION['access_token'] = $response->access_token;

    try {
        $client->authenticate($response->access_token, Github\Client::AUTH_HTTP_TOKEN);
    } catch (\Exception $e) {
        dp($e->getMessage());
    }

So there we have it.

I did try using the HttpClient of the php github api library but I was having some issues so chose a more simple solution.

Upvotes: 5

Andrew
Andrew

Reputation: 536

The problem is that you're using the code you receive after the user authenticates as a $token when you're supposed to use it to get an actual token. Make a post request to https://github.com/login/oauth/access_token with the client_id, client_secret, code (what you were using as the token), state, and redirect_uri.

You'll get back a response in this format access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&scope=user%2Cgist&token_type=bearer

There is this code in the HttpClient.php file that would make getting the token easier than cURLing

public function post($path, $body = null, array $headers = array())
{
    return $this->request($path, $body, 'POST', $headers);
}

https://developer.github.com/v3/oauth/#github-redirects-back-to-your-site

Upvotes: 4

Related Questions