首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在PHP/Laravel中没有来自Google客户端SDK的刷新标记

在PHP/Laravel中没有来自Google客户端SDK的刷新标记
EN

Stack Overflow用户
提问于 2018-08-14 10:43:14
回答 2查看 363关注 0票数 0

我整天都在为这个绞尽脑汁。不确定为什么我在生产中看到这个错误,但在本地测试时没有错误。

代码语言:javascript
复制
{
    "message": "refresh token must be passed in or set as part of setAccessToken",
    "exception": "LogicException",
    "file": "/var/www/laravel/vendor/google/apiclient/src/Google/Client.php",
    "line": 266,
    "trace": [
        {
            "file": "/var/www/laravel/vendor/google/apiclient/src/Google/Client.php",
            "line": 254,
            "function": "fetchAccessTokenWithRefreshToken",
            "class": "Google_Client",
            "type": "->"
        },
        {
            "file": "/var/www/laravel/app/Services/GoogleService.php",
            "line": 50,
            "function": "refreshToken",
            "class": "Google_Client",
            "type": "->"

       }
}

我的设置如下:

代码语言:javascript
复制
class GoogleService {

    protected $client;

    protected $service;

    function __construct() {
        /* Get config variables */
        putenv('GOOGLE_APPLICATION_CREDENTIALS=' . resource_path(config('google.credentials')));

        $scopes = array(
            ...
        );

        $this->client = new \Google_Client();
        $this->client->useApplicationDefaultCredentials();

        $this->client->setApplicationName(config('google.app_name'));

        $service_account = config('google.service_account_name');
        $this->client->setSubject($service_account);

        $this->client->setScopes($scopes);
        $this->client->setAccessType('offline');
        $this->client->setApprovalPrompt('force');

        $this->service = new \Google_Service_Directory($this->client);

        /* If we have an access token */
        if (Cache::has('service_token')) {
            $this->client->setAccessToken(Cache::get('service_token'));
        }

        if ($this->client->isAccessTokenExpired()) {
            $this->client->refreshToken(Cache::get('service_token'));
        }

        Cache::forever('service_token', $this->client->getAccessToken());
    }

    public function verify($user_id)
    {
        try {

            $result = $this->service->users->get($user_id);

        } catch (\Google_Service_Exception $e) {

            Log::error($e->getMessage());

            return response()->json(['error' => 'There was a general error : ' . $e->getMessage()], 404);

        }

        return $result;
    }

    public function get($user_id)
    {
        $optParams = array('projection' => 'full');
        $user = $this->service->users->get($user_id, $optParams);

        if ($user_id !== $user->primaryEmail) {
            return response()->json(['error' => 'Unauthorized - Bad credentials.'], 401);
        }

        return $user;
    }
}

我终于在mac上使用vagrant/homestead完成了本地工作,我试图加载到生产环境中,但无法运行它。我找到了其他一些关于错误的帖子,但它似乎只适用于客户端到服务器,在这种情况下是服务器到服务器。我想知道这是否会有所不同。

任何帮助都将不胜感激。

谢谢大家!

EN

回答 2

Stack Overflow用户

发布于 2018-08-14 11:23:31

第一次运行此代码时,service_token将不存在于缓存中,因此

  1. Cache::has('service_token')将返回null.

,and

  • Cache::get('service_token')将返回false

在此代码中:

代码语言:javascript
复制
/* If we have an access token */
if (Cache::has('service_token')) { // false
    $this->client->setAccessToken(Cache::get('service_token'));
}

if ($this->client->isAccessTokenExpired()) { // true
    $this->client->refreshToken(Cache::get('service_token'));
}

第一个条件将为false,因此不会调用setAccesstoken()。第二个条件将返回true,因为在未设置令牌的情况下,isAccessTokenExpired()返回true,因此您将把null传递给refreshToken()函数,该函数将抛出您得到的异常。

您的代码可能如下所示:

代码语言:javascript
复制
/* If we have an access token */
if (Cache::has('service_token')) {
    $this->client->setAccessToken(Cache::get('service_token'));

    if ($this->client->isAccessTokenExpired()) {
        $this->client->refreshToken(Cache::get('service_token'));
    }
} else {
    /* whatever code goes here to get the access token for the first time */
}
票数 0
EN

Stack Overflow用户

发布于 2018-08-15 04:15:56

这已经解决了。借助patricus

看起来我保存文件的位置在资源目录中,这似乎是不可读的。一旦移动到存储中,它就是可读的。我会努力为这个文件找到一个最终的安息之处。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51832999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档