一小时后,FOSOUserBundle + HWIOAuthBundle google accessstoken超时

时间:2013-09-10 05:51:15

标签: symfony-2.1 fosuserbundle hwioauthbundle

Iam使用FOSUserBundle + HWIOAuthBundle,并且在一小时后遇到有关accessstoken过期的问题。每次登录时,我都会在db中保存访问令牌。如何刷新我的访问令牌并在db中更新?有没有关于此的文件?

1 个答案:

答案 0 :(得分:1)

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

在获取联系人的服务中,它会尝试使用当前保存的令牌,如果不能解决问题,它会利用用户的刷新令牌生成新的访问令牌。

我的旅行让我进入了this之类的设置,但我没有在注册时捕获刷新令牌。所以,直到我想到这一点,我真的很高兴。

此外,如果您想要访问该刷新令牌,则在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);
    }

}

<强> FOSUBProvider

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