oauth-4-laravel icon indicating copy to clipboard operation
oauth-4-laravel copied to clipboard

Facebook requestAccessToken returns TokenResponseException

Open zaalbarxx opened this issue 10 years ago • 9 comments

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.

zaalbarxx avatar Jun 11 '14 16:06 zaalbarxx

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

alejoabella avatar Jun 11 '14 16:06 alejoabella

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.

zaalbarxx avatar Jun 11 '14 16:06 zaalbarxx

I am having the same problem

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

murum avatar Jun 15 '14 12:06 murum

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

murum avatar Jun 15 '14 12:06 murum

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

public function __construct( Oauth $oauth )
    {
        $this->oauth = $oauth;
        $this->oauth->setHttpClient('CurlClient');
    }

kreitje avatar Jun 26 '14 00:06 kreitje

@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?

murum avatar Jul 05 '14 18:07 murum

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

kreitje avatar Jul 05 '14 18:07 kreitje

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

murum avatar Jul 05 '14 18:07 murum

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.

kreitje avatar Jul 05 '14 18:07 kreitje