Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Basic login seems to not be working #1

Open
posabsolute opened this issue Jan 23, 2012 · 2 comments
Open

Basic login seems to not be working #1

posabsolute opened this issue Jan 23, 2012 · 2 comments

Comments

@posabsolute
Copy link

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

@bsatoris
Copy link

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

@sorbing
Copy link

sorbing commented Nov 7, 2013

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants