Json键缺少类型字段

时间:2017-10-19 11:13:42

标签: php api calendar

我尝试使用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):   谷歌\验证\ CredentialsLoader :: makeCredentials(' https://www.goo ...&#39 ;,   数组)#1   /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/src/Google/Client.php(1078):   谷歌\验证\ ApplicationDefaultCredentials :: getCredentials(显示' https://www.goo ...&#39)

     

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

我看到生成了一个令牌。

有人可以帮助我建立这种联系吗?

2 个答案:

答案 0 :(得分:1)

有些东西您没有看到。 单击下拉列表中的“创建凭据”,您将看到三个选项:

  1. API密钥
  2. OAuth客户端ID
  3. 服务帐户密钥

选择服务帐户密钥,选择您的项目名称,然后从此处下载json文件,然后将该json文件放在此处putenv('GOOGLE_APPLICATION_CREDENTIALS=SellerWizard.json');

答案 1 :(得分:0)

我解决了,JSON-key-file不包含type字段。但是我删除了一些陈述并回到了最小的数据。还添加了声明:

$client->setApprovalPrompt('force');