php - invalidargumentexception credentials.json" 不存在
问题描述
我正在尝试将 Google API 与工艺 cms 一起使用。从 Google 的日历中提取数据。我修改了 Google 在 Github 上的 quickstsrt.php。那行得通,但是当我将代码添加到我的 Craft 插件时,它再也找不到 JSON 文件了
我有相同的代码在工艺之外工作。我已经浏览了谷歌的结果,但找不到任何可行的解决方案。这可能是我授权我的谷歌帐户的原因,但就像我说的 PHP 在工艺之外工作
public function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
$client->setAuthConfig(credentials.json);
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
InvalidArgumentException: file "/home/mesicdev/www/craft/plugins/credentials.json" does not exist in /home/mesicdev/public_html/craft/plugins/calendarpuller/src/services/vendor/google/apiclient/src/Google/Client.php:870
堆栈跟踪:
0 /home/mesicdev/public_html/craft/plugins/calendarpuller/src/services/CalendarService.php(87): Google_Client->setAuthConfig('/home/mesicdev/...')
1 /home/mesicdev/public_html/craft/plugins/calendarpuller/src/services/CalendarService.php(60): campnonet\calendarpuller\services\CalendarService->getClient()
2 /home/mesicdev/public_html/craft/plugins/calendarpuller/src/controllers/CalendarController.php(63): campnonet\calendarpuller\services\CalendarService->getCalendarInfo()
3【内部函数】:campnonet\calendarpuller\controllers\CalendarController->actionDefault(true)
4 /home/mesicdev/public_html/craft/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
5 /home/mesicdev/public_html/craft/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
6 /home/mesicdev/public_html/craft/vendor/craftcms/cms/src/web/Controller.php(109): yii\base\Controller->runAction('default', Array)
7 /home/mesicdev/public_html/craft/vendor/yiisoft/yii2/base/Module.php(528):craft\web\Controller->runAction('default', Array)
8 /home/mesicdev/public_html/craft/vendor/craftcms/cms/src/web/Application.php(297): yii\base\Module->runAction('calendar-puller...', Array)
9 /home/mesicdev/public_html/craft/vendor/yiisoft/yii2/web/Application.php(103):craft\web\Application->runAction('calendar-puller...', Array)
10 /home/mesicdev/public_html/craft/vendor/craftcms/cms/src/web/Application.php(286): yii\web\Application->handleRequest(Object(craft\web\Request))
11 /home/mesicdev/public_html/craft/vendor/yiisoft/yii2/base/Application.php(386):craft\web\Application->handleRequest(Object(craft\web\Request))
12 /home/mesicdev/public_html/craft/web/index.php(21): yii\base\Application->run()
13 {主要}
解决方案
无论出于何种原因,我都错了,我在输入 json Thaks 的路径时忘记添加 /src 文件夹以寻求帮助
推荐阅读
- tensorflow - 为什么输入与层model_16不兼容?
- javascript - 如何制作一个按钮,按下时按钮会消失并弹出一个新的div
- python - vs代码终端说python 2.7,但我选择了3.8
- command - 如何从Stata的数据集中删除特定的观察结果?
- r - 如何使 klaR 的 greedy.wilks 函数在后 HCPC 数据帧上工作?
- javascript - 背景图像不适用于 Chrome for Mac 上的一个 div
- docker-compose - Docker-compose 按项目名称隔离网络
- swift - 在 TabView 中无法识别 editMode 更改
- android - Android:退出应用程序,要求第二次点击或忽略?
- java - Java Springboot 反应式存储库 - myReactiveRepository.insert(myMono) 和 myMono.flatmap(myRepository::save) 之间的区别