PHP 客户端库使用入门

要开始通过 PHP 客户端库使用 Google Photos Library API,您需要执行以下操作: 在开发环境中设置客户端库。 在此之前,请先启用 API 来配置您的项目 通过 Google API 控制台设置 OAuth 2.0 客户端 ID

您的应用会代表 Google 相册用户。例如,如果您在用户的 Google 相册媒体库或将媒体内容上传到用户的媒体库 Google 相册账号,用户便可通过 OAuth 2.0 协议。

OAuth 2.0 客户端 ID 可让您的应用用户登录账号、进行身份验证 从而使用 Library API。Library API 不支持 服务账号;要使用此 API,用户必须登录到有效的 Google 账号。

配置您的应用

启用 API

您必须先为项目启用 Library API,然后才能使用该 API。

  1. 转到 Google API 控制台
  2. 从菜单栏中选择一个项目或创建一个新项目。
  3. 要打开 Google API 库,请在导航菜单中选择 API 和服务 >媒体库
  4. 搜索“Google Photos Library API”。选择正确的结果并点击 启用

请求 OAuth 2.0 客户端 ID

请按照以下步骤申请 OAuth 客户端 ID 并为其配置 OAuth 客户端 ID 应用。本示例使用的应用将整个 OAuth 流程 由服务器端处理,如我们的示例中所示设置流程可能会有所不同 适用于其他实现场景

  1. 转到 Google API 控制台 并选择您的项目。
  2. 在菜单中选择 API 和服务 >凭据
  3. 凭据页面上,点击创建凭据 >OAuth 客户端 ID
  4. 选择您的应用类型。在此示例中,应用类型是 Web 应用
  5. 注册允许您的应用从哪些来源访问 Google API 如下所示:

    1. 如需识别客户端 ID,请输入名称。
    2. 已获授权的 JavaScript 来源字段中,输入您 应用。此字段不允许使用通配符。

      您可以输入多个源,以允许您的应用在不同平台上运行 网域或子网域您输入的网址可用于 发起 OAuth 请求。

      以下示例显示了一个本地开发网址(我们的示例使用 localhost:8080)和一个正式版网址。

      http://localhost:8080
      https://myproductionurl.example.com
      
    3. 已获授权的重定向 URI 字段是接收 OAuth 2.0 服务器发出的响应请求通常情况下,这包括您的 开发环境,并指向应用中的路径。

      http://localhost:8080/auth/google/callback
      https://myproductionurl.example.com/auth/google/callback
      
    4. 点击创建

  1. 在随即显示的 OAuth 客户端对话框中,下载 JSON 文件 包含您的客户端配置客户详细信息包括 以下:

    • 客户端 ID
    • 客户端密钥

    此 JSON 文件稍后将用于设置 适用于 PHP 的 Google Auth 库,可以与此客户端库结合使用。

在启动访问 Library API 的公共应用之前, 您的应用必须经过 Google 审核。“未经验证的应用”消息出现在 测试您的应用时,该屏幕会展示,直到 经过验证

设置客户端库

PHP 客户端库可为您处理所有后端 API 调用,并公开 对象,包括一些常见 API 任务的代码示例。 首先,下载并安装适用于 PHP 的 Google Photos Library API 客户端库以及 从 GitHub 获取依赖项。 然后,设置适用于 PHP 的 OAuth2 凭据。

下载选项

使用 Composer 将该库作为依赖项添加到开发环境中。运行 将库添加到您的项目配置中并下载 放入 vendor/ 目录。

composer require google/photos-library

或者 克隆 代码库下载 tarball

设置适用于 PHP 的 OAuth2 凭据

此客户端库可与 Google Auth 库 PHP。如需更多信息 请参阅将 OAuth 2.0 与 Google API 客户端库结合使用 PHP

在设置时使用 auth 库返回的身份验证凭据 PhotosLibraryClient

试用一些示例

尝试使用以下代码,使用 PHP 客户端库进行首次 API 调用。

use Google\Auth\Credentials\UserRefreshCredentials;
use Google\Photos\Library\V1\PhotosLibraryClient;
use Google\Photos\Library\V1\PhotosLibraryResourceFactory;

try {
    // Use the OAuth flow provided by the Google API Client Auth library
    // to authenticate users. See the file /src/common/common.php in the samples for a complete
    // authentication example.
    $authCredentials = new UserRefreshCredentials( /* Add your scope, client secret and refresh token here */ );

    // Set up the Photos Library Client that interacts with the API
    $photosLibraryClient = new PhotosLibraryClient(['credentials' => $authCredentials]);

    // Create a new Album object with at title
    $newAlbum = PhotosLibraryResourceFactory::album("My Album");

    // Make the call to the Library API to create the new album
    $createdAlbum = $photosLibraryClient->createAlbum($newAlbum);

    // The creation call returns the ID of the new album
    $albumId = $createdAlbum->getId();
} catch (\Google\ApiCore\ApiException $exception) {
    // Error during album creation
} catch (\Google\ApiCore\ValidationException $e) {
    // Error during client creation
    echo $exception;
}

还有更多 GitHub 上的示例 供您尝试。