【发布时间】:2017-10-19 11:13:41
【问题描述】:
我尝试使用 google api for php,版本 2.2.0 连接到我的 Google 日历。直到现在我都无法成功连接到我的日历,这非常令人沮丧。
此时我收到以下致命错误:
致命错误:未捕获的 InvalidArgumentException:缺少 json 键 中的类型字段 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/vendor/google/auth/src/CredentialsLoader.php:123 堆栈跟踪:#0 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/vendor/google/auth/src/ApplicationDefaultCredentials.php(154): Google\Auth\CredentialsLoader::makeCredentials('https://www.goo...', 阵列)#1 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/src/Google/Client.php(1078): Google\Auth\ApplicationDefaultCredentials::getCredentials('https://www.goo...')
2 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/src/Google/Client.php(365):
Google_Client->createApplicationDefaultCredentials() #3 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/src/Google/Client.php(786): Google_Client->authorize() #4 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/src/Google/Service/Resource.php(232): Google_C 在 /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/vendor/google/auth/src/CredentialsLoader.php 在第 123 行
我使用这个代码:
require_once WP_PLUGIN_DIR . '/google-api-php-client-2.2.0/vendor/autoload.php';
require_once WP_PLUGIN_DIR . '/google-api-php-client-2.2.0/src/Google/Client.php';
require_once WP_PLUGIN_DIR . "/google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/Calendar/Resource/CalendarList.php";
require_once WP_PLUGIN_DIR . "/google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/Oauth2.php";
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . dirname(__FILE__) . '/oauth-credentials.json');
$client = new Google_Client();
$redirect_uri = 'https://myredirect-uri';
$client->setRedirectUri($redirect_uri);
$client->setRedirectUri($redirect_uri);
$client->setClientId('myclient-di.apps.googleusercontent.com');
$client->setClientSecret('myclient-secret');
$client->setAccessType("offline"); // offline access
$client->setIncludeGrantedScopes(true); // incremental auth
$client->addScope(Google_Service_Calendar::CALENDAR);
$user_to_impersonate = 'owner-of-agenda@gmail.com';
$client->setSubject($user_to_impersonate);
$client->setAuthConfig(GOOGLE_APPLICATION_CREDENTIALS);
$client->useApplicationDefaultCredentials();
if (file_exists(CREDENTIALS_PATH)) {
$token = file_get_contents(CREDENTIALS_PATH);
echo "<h4>_Token</h4>";
var_dump($token);
$client->setAccessToken($token);
}
echo "<br><br>CLIENT";
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
var_dump($calendarList);
echo "<h4>End of example</h4>";
我看到生成了一个令牌。
有人可以帮我建立这种联系吗?
【问题讨论】:
-
在我看来
/oauth-credentials.json中的 json blob 缺少type字段。由于您没有包含该文件的内容,因此很难调试,但我会先将其与 Google 期望在其中找到的内容进行比较。 -
文件内容直接来自google,所以我觉得应该没问题。可能是我没有配置设置不行...... pffftt
-
抱歉,文件内容:{"web":{"client_id":"my-client-id","project_id":"my-project-id","auth_uri":" accounts.google.com/o/oauth2/…}}
-
我终于解决了; json-key-file 不包含 type 字段。但是我删除了一些语句并回到了最少的数据。还添加了语句:$client->setApprovalPrompt('force');