dsyph3r/github-api3-php

Basic login seems to not be working

posabsolute opened this issue · 2 comments

When I try to login the script throw no errors,

but I am obviously not logged in as

var_dump($user->emails()->all());

report unauthorized

using
$user->setCredentials(new Authentication\Basic('username', 'password'));
$user->login();

I had the same issue -- it appears to be a design problem because authentication is not abstracted from Api. So for example, when you do:

$user = new User();
$user->setCredentials(new Authentication\Basic('username', 'password'));

/* This causes $this->authenticated = TRUE and 
$this->authenticator to be an instance of AuthenticationInterface
in the Api class. All is good. */

//this will work
var_dump($user->isFollowing('octocat'));

//this will not
var_dump($user->emails()->all());

Why won't the last call work? Because User::emails() instantiates a new instance of Email, which also extends from Api. So the instance variables $this->authenticator and $this->authenticated are set to their default values of null and FALSE for the instance of Api that we are now using. So when Api::doRequest() runs the authentication information is no longer there, and not passed with the request.

For a one-off project I was doing I just implemented a hack in Api::doRequest() to set the credentials there, ensuring they are present in the requests I needed to make. The real solution is to abstract the authentication stuff out of Api.

    protected function doRequest($method, $url, $params, $headers)
    {
        $request = $this->transport->createRequest();

        $request->setMethod($method);
        $request->fromUrl($url);
        $request->addHeaders($headers);
        $request->setContent($params);
        //begin hack
        $this->setCredentials(new Authentication\Basic('username', 'password'));
        $this->login();
        //end hack
        if ($this->isAuthenticated() && null !== $this->authenticator)
        {
            $request = $this->authenticator->authenticate($request);
        }

        return $this->transport->send($request);
    }

Error: "Fatal error: Call to undefined method Buzz\Browser::createRequest() in /www/project/vendor/dsyph3r/github-api3-php/lib/GitHub/API/Api.php on line 232" for OAuth Credentials type.