【问题标题】:Json key is missing the type fieldJson 键缺少类型字段
【发布时间】: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');

标签: php api calendar


【解决方案1】:

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

$client-&gt;setApprovalPrompt('force');

【讨论】:

    【解决方案2】:

    有一些你没有看到的东西。 有一个下拉 Create Credentials 点击它,你会看到三个选项:

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

    选择服务账户密钥,选择你的项目名称并从那里下载json文件,把那个json文件放在这里,putenv('GOOGLE_APPLICATION_CREDENTIALS=SellerWizard.json');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-21
      • 2016-01-11
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 2016-08-29
      相关资源
      最近更新 更多