php JSON键缺少类型字段

g2ieeal7  于 2023-06-28  发布在  PHP
关注(0)|答案(3)|浏览(97)

我尝试使用Google API for PHP 2.2.0版本连接到Google日历。直到现在,我还没有成功地连接到我的日历,这是非常令人沮丧的。
此时,我收到以下致命错误:
致命错误:Uncaught InvalidArgumentException:json key is missing the type field in /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/vendor/google/auth/src/CredentialsLoader.php:123堆栈跟踪:Copyright © 2018 - 2019 www. cn-servi.com. All rights reserved.沪ICP备15024552号-1 Google\Auth\CredentialsLoader::makeCredentials('https://www.goo...',Array)#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):Copyright © 2018 www.cn-servi.com.cn All Rights Reserved.版权所有:深圳市联科电子有限公司
我使用这个代码:

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>";

我看到生成了一个令牌。
有人能帮我联系一下吗?

z31licg0

z31licg01#

我解决了这个问题,JSON密钥文件不包含类型字段。但我删除了一些语句,回到了数据的最小值。还添加了声明:
$client->setApprovalPrompt('force');

bd1hkmkf

bd1hkmkf2#

有些事你没看到有一个下拉菜单Create Credentials,点击它,你会看到三个选项:

  1. API密钥
  2. OAuth客户端ID
    1.服务帐户密钥
    选择服务帐户密钥,选择项目名称并从那里下载JSON文件,将该JSON文件放在这里,putenv('GOOGLE_APPLICATION_CREDENTIALS=SellerWizard.json');
kpbwa7wx

kpbwa7wx3#

我今天都快疯了。
就我而言,它是一个用于PHP7的较旧的PHP客户端API,用于在文件tokens.json中查找client_id和secret_id。
setClientId()方法没有接受它,我的意思是,它被接受了,但没有在操作中使用。
我的解决方案是在tokens.json中添加client_id和client_secret。
这在使用其他版本的API时不会发生。

相关问题