繁体   English   中英

google-api-php-client:无效的客户端密钥JSON文件

[英]google-api-php-client: Invalid client secret JSON file

似乎用于PHP的google-api-php-client的最新版本不符合文档@ https://developers.google.com/drive/web/examples/php

查看src代码我发现它正在寻找下载的JSON中的setAuthConfigFile()方法无法找到的键:client_secret,installed,web,redirect_uris(其他?)在下载的JSON中不存在。 仅存在private_key_id,private_key,client_email,client_id和type。

代码和文档似乎真的杂乱无章,不同步......不会是谷歌的第一个。 有没有人最近使用该库使OAuth工作?

“服务帐户”“网络应用程序”之间存在差异,无法调用API。 当您创建“服务帐户”时,您将获得上述文件,带有private_keyclient_emailclient_id等的JSON文件。

创建Web应用程序时,您将获得client_idclient_secretredirect_uri等。

我建议阅读这些页面以选择您需要的密钥和登录名(在两个页面上,您都可以找到将其集成到PHP中的示例):

您可以使用适用于PHP的Google API客户端库创建使用OAuth 2.0授权访问Google API的Web服务器应用程序。 OAuth 2.0允许用户与应用程序共享特定数据,同时保持其用户名,密码和其他信息的私密性。 例如,Web应用程序可以使用OAuth 2.0获取用户在其Google Drives中存储文件的权限。

https://developers.google.com/api-client-library/php/auth/web-app

通常,当应用程序使用Google API处理自己的数据而不是用户的数据时,应用程序会使用服务帐户。 例如,使用Google Cloud Datastore进行数据持久性的应用程序将使用服务帐户对其对Google Cloud Datastore API的调用进行身份验证。

https://developers.google.com/api-client-library/php/auth/service-accounts

php库中有一个新功能,它接近于此,但不允许设置sub,因此总是授权失败。 所以,首先将src / Google / Client.php中的php库函数loadServiceAccountJson更新为:

  public function loadServiceAccountJson($jsonLocation, $scopes)
  {
    $data = json_decode(file_get_contents($jsonLocation));
    if (isset($data->type) && $data->type == 'service_account') {
      // Service Account format.
      $cred = new Google_Auth_AssertionCredentials(
          $data->client_email,
          $scopes,
          $data->private_key,
          'notasecret',
          'http://oauth.net/grant_type/jwt/1.0/bearer',
          $data->sub
      );
      return $cred;
    } else {
      throw new Google_Exception("Invalid service account JSON file.");
    }
  }

然后,将值sub添加到服务器auth json文件中的数据:

{
  "private_key_id": "removed",
  "private_key": "-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n",
  "client_email": "removed",
  "client_id": "removed",
  "redirect_uris":[your urls here],
  "type": "service_account",
  "sub": "valid.user@google.domain.com"
}

现在,获得授权:

$credentials = $client->loadServiceAccountJson('serverauth.json',"https://www.googleapis.com/auth/admin.directory.user.readonly");
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion();
}

其中serverauth.json是从您要使用的服务帐户下载的JSON密钥文件,并将子行添加到。

最后,创建一个Directory实例并对其进行查询:

$service = new Google_Service_Directory($client);
$optParams = array(
        'domain' => 'google.domain.com',
        'orderBy' => 'email',
        'viewType' => 'domain_public',
        'query' => "givenName:'Joe' familyName:'Schmoe Jr'"
);
$results = $service->users->listUsers($optParams);
$users = $results->getUsers();

print_r($users);

我了解您的风险,您的Google API存在问题。 在Google API控制台中,有3种json文件,一种是Web,第二种是Service,最后一种是Installed。 您需要使用的选择是安装,因为您将获得密钥,已安装或其他..

1)“CREDENTIALS_PATH”应指向不存在的文件(在可写路径中)

2)“CLIENT_SECRET_PATH”应指向“ID客户端OAuth 2.0”凭据文件,该文件是在Api Credential部分中从Google控制台创建和下载的。

对于服务器端PHP脚本,与您的一样,在创建“ID客户端OAuth 2.0”记录时要注意:在创建向导中,您应该选择“其他”类型的应用程序而不是“Web”类型。

问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM