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

时间:2015-01-18 05:05:55

标签: php oauth google-api-php-client

似乎用于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。

代码和文档似乎非常混乱且不同步......不会是Google的第一个。有没有人最近使用该库让OAuth工作?

4 个答案:

答案 0 :(得分:7)

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

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

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

  

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

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

答案 1 :(得分:2)

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.");
    }
  }

然后,为服务器auth json文件中的数据添加一个值sub:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

1)" CREDENTIALS_PATH"应该指向一个不存在的文件(在可写路径中)

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

对于像你这样的服务器端php脚本,在创建" ID客户端OAuth 2.0"时要注意。记录:在创建向导中,您应该选择"其他"应用程序的类型,而不是" web"类型。

此致