symfony-2.1 - FOSOUserBundle + HWIOAuthBundle google accesstoken 一小时后超时

标签 symfony-2.1 fosuserbundle hwioauthbundle

我正在使用 FOSUserBundle + HWIOAuthBundle 并且遇到关于 accesstoken 在一小时后过期的问题。在这里,我在每次登录时将访问 token 保存在数据库中。如何刷新我的访问 token 并在数据库中更新?有没有这方面的文件?

最佳答案

我最近遇到了这个问题!正在使用存储的访问 token 获取联系人。

在获取联系人的服务中,它会尝试使用当前保存的 token ,如果没有成功,它会利用用户的刷新 token 生成新的访问 token 。

我的旅行让我找到了类似 this 的设置,但我没有在注册时捕获刷新 token 。所以在我弄明白之前,我很兴奋。

此外,如果您想访问该刷新 token ,则在 HWIOauth 中定义 google 资源时,您需要在范围内请求“离线”访问权限。

联系人检索服务:

<?php

namespace Acme\DemoBundle\Services;
use Acme\DemoBundle\Entity\User;

class GoogleContactRetriever
{

    private $user;

    private $buzz;

    public function __construct($buzz, $googleId, $googleSecret, $userProvider)
    {
        $this->buzz = $buzz;
        $this->googleId = $googleId;
        $this->googleSecret = $googleSecret;
        $this->userProvider = $userProvider;
    }

    public function setUser($user)
    {
        $this->user = $user;
    }

    public function requestContacts()
    {
        return $this->buzz->get( "https://www.google.com/m8/feeds/contacts/default/full?access_token=".$this->user->getGoogleAccessToken() );
    }

    public function retrieveUserContacts()
    {
        $response = $this->requestContacts();

        $headers = $response->getHeaders();

        if ($headers[0] != 'HTTP/1.0 200 OK') {
            $this->refreshAccessToken($this->user);
            $response = $this->requestContacts();
        }

        return $this->parseResponseString($response->getContent());
    }

    // ...

    public function refreshAccessToken($user)
    {
        $refreshToken = $user->getGoogleRefreshToken();

        $response = $this->buzz->post( "https://accounts.google.com/o/oauth2/token", array(),
            "refresh_token=$refreshToken&client_id={$this->googleId}&client_secret={$this->googleSecret}&grant_type=refresh_token"
        );

        $responseContent = json_decode($response->getContent());

        $this->userProvider->updateGoogleAccessToken($user, $responseContent->access_token);
    }

}

FOSUB提供者

<?php
namespace NYW\Bundle\CoreBundle\Security\Core\User;

use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\Security\Core\User\UserInterface;

class FOSUBUserProvider extends BaseClass
{

    /**
     * {@inheritdoc}
     */
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        $serviceName = $response->getResourceOwner()->getName();

        $avatar = null;
        $username = $response->getUsername();

        $email = $response->getEmail();
        $user = $this->userManager->findUserByEmail($email);
        //when the user is registrating
        if (null === $user) {

            $service = $response->getResourceOwner()->getName();

            $setter = 'set'.ucfirst($service);
            $setter_id = $setter.'Id';
            $setter_token = $setter.'AccessToken';
            // create new user here
            $user = $this->userManager->createUser();
            $user->$setter_id($email);
            $user->$setter_token($response->getAccessToken());

            switch ($service) {
                case 'google':
                    $refreshToken = $response->getRefreshToken();
                    $user->setGoogleRefreshToken($refreshToken);
                break;
            }

            //I have set all requested data with the user's username
            //modify here with relevant data
            $user->setUsername($username);
            $user->setEmail($email);
            $user->setPassword($email);
            $user->setEnabled(true);
            $this->userManager->updateUser($user);

            return $user;
        }

        //We used to call the parent's loadUserByOAuthUserResponse method here..

        $setter = 'set' . ucfirst($serviceName) . 'AccessToken';

        //update access token
        $user->$setter($response->getAccessToken());

        return $user;
    }

    public function updateGoogleAccessToken($user, $token)
    {
        $user->setGoogleAccessToken($token);
        $this->userManager->updateUser($user);
    }

}

config.yml

services:
    adb.user_provider:
        class: "Acme\DemoBundle\Security\Cure\FOSUBUserProvider"
            arguments: [@fos_user.user_manager,{facebook: facebook_id, google: google_id}]

hwi_oauth:
    connect:
        account_connector: adb.user_provider
    resource_owners:
        google:
            scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.google.com/m8/feeds"

关于symfony-2.1 - FOSOUserBundle + HWIOAuthBundle google accesstoken 一小时后超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18711540/

相关文章:

symfony - Hwi oauth bundle 和 Symfony 3.4 无法 Autowiring 服务 : How to use hwi/oauth-bundle in symfony 3. 4 + FOSUserBundle

symfony - 无法在产品中找到模板

php - Symfony POST 变量总是空的

Symfony2 : How to get user Object inside controller when using FOSUserBundle?

security - FOSUserBundle - 使用不同编码的密码登录 Symfony2

php - HWIOAuthBundle 第一个示例的问题

php - 在 HWIOAuthBundle 中调用 loadUserByOAuthUserResponse() 时

arrays - 在 Symfony 2.1 语义配置中允许数组(具有默认值)或 null

Symfony2 选择学说中的一列

php - Symfony2 - FOSUserBundle - 多个登录位置