我正在尝试通过工艺cms使用Google API。从Google从我的日历中提取数据。我修改了Google在Github上拥有的quickstsrt.php。那行得通,但是当我将代码添加到Craft插件时,它不再能够找到JSON文件
我在手工艺之外也有相同的代码。我浏览了google结果,却找不到任何可行的解决方案。这可能就是为什么我授权了我的Google帐户的原因,但就像我说过的那样,PHP可以在craft之外运行
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 :(得分:0)
您在此行中有一个错误
$client->setAuthConfig(credentials.json);
这应该是真实文件的路径。
改为使用此:
$client->setAuthConfig("/path/to/credentials.json");
答案 1 :(得分:0)
由于任何原因我错了,我在输入json Thaks的路径时忘记了添加/ src文件夹以获取帮助