oauth-4-laravel
oauth-4-laravel copied to clipboard
Facebook requestAccessToken returns TokenResponseException
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.
i don't know exactly why you are using $fb = $this->oauth->consumer( but try this: $fb = OAuth::consumer('Facebook','http://someurl.com/');
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.
I am having the same problem
$token = $fb->requestAccessToken( $code ); // This row fails.
A note that can be good to know.. It worked before I turned this option "on" on facebook.
I found that by switching to the CurlClient it will work.
public function __construct( Oauth $oauth )
{
$this->oauth = $oauth;
$this->oauth->setHttpClient('CurlClient');
}
@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?
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 Thanks mate, it works for me now. But I guess this should be fixed anyhow or?
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.