2017-10-19 90 views
-1

我嘗試使用PHP版本2.2.0的google api連接到我的Google日曆。到目前爲止,我沒有成功地連接到我的日曆,這是非常令人沮喪的。Json鍵缺少類型字段

這時我收到以下致命錯誤:

Fatal error: 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 Stack trace: #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 ...', 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): Google_C in /home/servi471/public_html/wp-content/plugins/google-api-php-client-2.2.0/vendor/google/auth/src/CredentialsLoader.php on line 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 = '[email protected]'; 
$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>"; 

我看到生成一個令牌。

有人可以幫助我建立這種聯繫嗎?

+0

在我看來就像在'/ OAuth的credentials.json'缺少'type'場json的斑點。由於您沒有包含該文件的內容,因此很難進行調試,但我首先將其與Google期望的內容進行比較。 –

+0

該文件的內容直接來自谷歌,所以我認爲它應該沒問題。可能是因爲我沒有配置設置不好..... pffftt –

+0

對不起,文件內容: {「web」:{「client_id」:「my-client-id」,「project_id」:「my -project-ID」, 「auth_uri」: 「https://accounts.google.com/o/oauth2/auth」, 「token_uri」: 「https://accounts.google.com/o/oauth2/token」 「auth_provider_x509_cert_url」: 「https://www.googleapis.com/oauth2/v1/certs」, 「client_secret」: 「我的客戶端祕密」, 「redirect_uris中」: 「https://開頭我重定向-URI」 ],「javascript_origins」:[「https://my-website.nl」]}} –

回答

0

我解決了它,JSON密鑰文件不包含類型字段。但我刪除了一些陳述,並回到了最低限度的數據。還添加了聲明:

$client->setApprovalPrompt('force');