Php 获取OAuth凭据时出错:“0”;OAutheException:此授权码已被使用;
我找到了很多关于这个问题的教程。但是没有人解决我的问题。所以,我正在尝试Symfony 4,以便在OAuth2 Facebook上跟进 当我点击我的按钮“与Facebook连接”时,我会看到一个空白页面,上面写着: 获取OAuth凭据时出错:“OAuthException:此授权 代码已被使用。” 我在一些教程中看到了关于accessToken、longliveAccessToken等的问题 但是我不知道在我的代码中要做什么来解决这个问题 以下是我的FacebookAuthenticator.php代码:Php 获取OAuth凭据时出错:“0”;OAutheException:此授权码已被使用;,php,facebook,symfony,authentication,oauth,Php,Facebook,Symfony,Authentication,Oauth,我找到了很多关于这个问题的教程。但是没有人解决我的问题。所以,我正在尝试Symfony 4,以便在OAuth2 Facebook上跟进 当我点击我的按钮“与Facebook连接”时,我会看到一个空白页面,上面写着: 获取OAuth凭据时出错:“OAuthException:此授权 代码已被使用。” 我在一些教程中看到了关于accessToken、longliveAccessToken等的问题 但是我不知道在我的代码中要做什么来解决这个问题 以下是我的FacebookAuthenticator.p
<?php
namespace App\Security;
use App\Entity\User; // your user entity
use Doctrine\ORM\EntityManagerInterface;
use KnpU\OAuth2ClientBundle\Security\Authenticator\SocialAuthenticator;
use KnpU\OAuth2ClientBundle\Client\Provider\FacebookClient;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
class FacebookAuthenticator extends SocialAuthenticator
{
private $clientRegistry;
private $em;
public function __construct(ClientRegistry $clientRegistry, EntityManagerInterface $em)
{
$this->clientRegistry = $clientRegistry;
$this->em = $em;
}
public function supports(Request $request)
{
// continue ONLY if the current ROUTE matches the check ROUTE
return $request->attributes->get('_route') === 'connect_facebook_check';
}
public function getCredentials(Request $request)
{
// this method is only called if supports() returns true
// For Symfony lower than 3.4 the supports method need to be called manually here:
// if (!$this->supports($request)) {
// return null;
// }
return $this->fetchAccessToken($this->getFacebookClient());
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
/** @var FacebookUser $facebookUser */
$facebookUser = $this->getFacebookClient()
->fetchUserFromToken($credentials);
// 1) have they logged in with Facebook before? Easy!
$existingUser = $this->em->getRepository(User::class)
->findOneBy(['facebookId' => $facebookUser->getId()]);
if ($existingUser) {
return $existingUser;
}
// 2) do we have a matching user by email?
$user = $this->em->getRepository(User::class)
->findOneBy(['email' => $email]);
// 3) Maybe you just want to "register" them by creating
// a User object
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*_";
if(!$user){
$user = new User();
}
$user->setFacebookId($facebookUser->getId());
$user->setUsername($facebookUser->getEmail());
$user->setPassword(password_hash(substr( str_shuffle( $chars ), 0, 10), PASSWORD_DEFAULT));
$user->setPrenom($facebookUser->getFirstName());
$user->setNom($facebookUser->getLastName());
$user->setEmail($facebookUser->getEmail());
$user->setEnabled(true);
$user->setSocialAuthentication(true);
$this->em->persist($user);
$this->em->flush();
return $user;
}
/**
* @return FacebookClient
*/
private function getFacebookClient()
{
return $this->clientRegistry
// "facebook_main" is the key used in config/packages/knpu_oauth2_client.yaml
->getClient('facebook_main');
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
// on success, let the request continue
return null;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$message = strtr($exception->getMessageKey(), $exception->getMessageData());
return new Response($message, Response::HTTP_FORBIDDEN);
}
/**
* Called when authentication is needed, but it's not sent.
* This redirects to the 'login'.
*/
public function start(Request $request, AuthenticationException $authException = null)
{
return new RedirectResponse(
'/connect/', // might be the site, where users choose their oauth provider
Response::HTTP_TEMPORARY_REDIRECT
);
}
}
我也有同样的问题,我用这种方式引用了long-live访问令牌。这可能是一个解决办法。给你
$client = $clientRegistry->getClient('facebook_main');
$accessToken = $client->getAccessToken();
if ($accessToken && !$accessToken->getToken()) {
dump("User is not found!"); die;
}
$provider = $client->getOAuth2Provider();
$longLivedToken = $provider->getLongLivedAccessToken($accessToken);
//I get the user by using long lived token
$facebookUser = $client->fetchUserFromToken($longLivedToken);
我也有同样的问题,我用这种方式引用了long-live访问令牌。这可能是一个解决办法。给你
$client = $clientRegistry->getClient('facebook_main');
$accessToken = $client->getAccessToken();
if ($accessToken && !$accessToken->getToken()) {
dump("User is not found!"); die;
}
$provider = $client->getOAuth2Provider();
$longLivedToken = $provider->getLongLivedAccessToken($accessToken);
//I get the user by using long lived token
$facebookUser = $client->fetchUserFromToken($longLivedToken);
您遇到的第一个错误是: 获取OAuth凭据时出错:“OAutheException:此授权代码已被使用。” 这是因为您重新加载了页面(例如更改代码和刷新之后)。错误消息说Url参数中的身份验证代码以前使用过。 而是刷新你的站点1。返回按钮所在的站点,然后单击2。通过单击FB登录按钮获取新的身份验证代码
现在,此错误已消失,您可以继续调试代码。出现的第一个错误: 获取OAuth凭据时出错:“OAutheException:此授权代码已被使用。” 这是因为您重新加载了页面(例如更改代码和刷新之后)。错误消息说Url参数中的身份验证代码以前使用过。 而是刷新你的站点1。返回按钮所在的站点,然后单击2。通过单击FB登录按钮获取新的身份验证代码
现在,此错误已消失,您可以继续调试代码。谢谢您的回答。我已经在Facebook控制器中尝试了你的代码,但是我没有在这个类中使用“$facebookUser”。我已经将这部分代码放在了connectCheckAction中,对吗?它应该在FacebookAuthenticator类中。我记得。嗨,默特,我已经把你的部分代码放在FacebookAuthenticator中,在getUser方法中,就像我放在编辑2中一样,仍然不起作用,我收到了相同的消息。谢谢你的帮助,兄弟。谢谢你的回答。我已经在Facebook控制器中尝试了你的代码,但是我没有在这个类中使用“$facebookUser”。我已经将这部分代码放在了connectCheckAction中,对吗?它应该在FacebookAuthenticator类中。我记得。嗨,默特,我已经把你的部分代码放在FacebookAuthenticator中,在getUser方法中,就像我放在编辑2中一样,仍然不起作用,我收到了相同的消息。谢谢你的帮助,兄弟。