artdarek / oauth-4-laravel

OAuth Service Provider for Laravel 4
684 stars 216 forks source link

Facebook requestAccessToken returns TokenResponseException #75

Open zaalbarxx opened 10 years ago

zaalbarxx commented 10 years ago

Hello! I have a problem with logging in with Facebook. I can go redirect to authorization url and return to callback. But when I come to the this piece of code I get exception

$code = Input::get('code');
        $fb = $this->oauth->consumer('Facebook','http://someurl.com/');
        if(!empty($code)){
            $fb->requestAccessToken($code); //TokenResponseException
            $facebookUser = json_decode($fb->request('/me'), true);

Exception message is 'Failed to request resource'. Any ideas how to make it work ? All other providers are working just fine. Facebook is the only problem here.

alejoabella commented 10 years ago

i don't know exactly why you are using $fb = $this->oauth->consumer( but try this: $fb = OAuth::consumer('Facebook','http://someurl.com/');

zaalbarxx commented 10 years ago

I am using $this->oauth, because I am injecting OAuth service into my controller. And like I said any other providers like Twitter etc. are working.

murum commented 10 years ago

I am having the same problem

$token = $fb->requestAccessToken( $code ); // This row fails. image

murum commented 10 years ago

A note that can be good to know.. It worked before I turned this option "on" on facebook. image

kreitje commented 10 years ago

I found that by switching to the CurlClient it will work.

public function __construct( Oauth $oauth )
    {
        $this->oauth = $oauth;
        $this->oauth->setHttpClient('CurlClient');
    }
murum commented 10 years ago

@kreitje Where did you add this code? I'm not working with a oauth the same way as you I guess.

Did you add that code in the controller where you're logging in a user or how does it work?

kreitje commented 10 years ago

In my controller that logs you in. Instead of using OAuth::consumer('Facebook') or whatever, I am injecting Oauth into the controller so you would use $this->oauth->consumer('Facebook')

<?php
use Artdarek\OAuth\OAuth;

class AuthController extends BaseController {

    protected $oauth;

    public function __construct( Oauth $oauth )
    {
        $this->oauth = $oauth;
        $this->oauth->setHttpClient('CurlClient');
    }
}
murum commented 10 years ago

@kreitje Thanks mate, it works for me now. But I guess this should be fixed anyhow or?

kreitje commented 10 years ago

It probably should be. I have had the issue for quite some time now on several projects. I have dug in a few times but couldn't find the issue before I had to move on.