我整天都在为这个绞尽脑汁。不确定为什么我在生产中看到这个错误,但在本地测试时没有错误。
{
"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": "->"
}
}
我的设置如下:
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完成了本地工作,我试图加载到生产环境中,但无法运行它。我找到了其他一些关于错误的帖子,但它似乎只适用于客户端到服务器,在这种情况下是服务器到服务器。我想知道这是否会有所不同。
任何帮助都将不胜感激。
谢谢大家!
发布于 2018-08-14 11:23:31
第一次运行此代码时,service_token
将不存在于缓存中,因此
Cache::has('service_token')
将返回null
.,and
Cache::get('service_token')
将返回false
在此代码中:
/* 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()
函数,该函数将抛出您得到的异常。
您的代码可能如下所示:
/* 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 */
}
发布于 2018-08-15 04:15:56
这已经解决了。借助patricus
看起来我保存文件的位置在资源目录中,这似乎是不可读的。一旦移动到存储中,它就是可读的。我会努力为这个文件找到一个最终的安息之处。
https://stackoverflow.com/questions/51832999
复制相似问题