開始使用 PHP 用戶端程式庫

如要開始透過 PHP 用戶端程式庫使用 Google Photos Library API,您必須在開發環境中設定用戶端程式庫。開始之前,請先透過 Google API 控制台啟用 API,並設定 OAuth 2.0 用戶端 ID,以設定專案

您的應用程式會代表 Google 相簿使用者與 Google 相簿互動。舉例來說,當您在使用者的 Google 相簿相片庫中建立相簿,或將媒體項目上傳至使用者的 Google 相簿帳戶時,使用者會透過 OAuth 2.0 通訊協定授權這些 API 要求。

OAuth 2.0 用戶端 ID 可讓應用程式使用者登入、驗證,然後再使用 Library API。Library API 不支援服務帳戶;如要使用這個 API,使用者必須登入有效的 Google 帳戶。

設定應用程式

啟用 API

您必須先為專案啟用 Library API,才能使用該程式庫。

  1. 前往 Google API 控制台
  2. 在選單列中,選取專案或建立新專案。
  3. 如要開啟 Google API 程式庫,請在導覽選單中依序選取「API 和服務」>「程式庫」
  4. 搜尋「Google Photos Library API」。選取正確的結果,然後按一下「啟用」

要求 OAuth 2.0 用戶端 ID

請按照下列步驟,要求取得 OAuth 用戶端 ID,並為您的應用程式設定該 ID。這個範例使用的應用程式在伺服器端處理整個 OAuth 流程,就如我們的範例中所示。設定程序可能因其他導入情境而異。

  1. 前往 Google API 控制台,然後選取您的專案。
  2. 在選單中依序選取「API 和服務」>「憑證」
  3. 在「憑證」頁面上,按一下「建立憑證」>「OAuth 用戶端 ID」
  4. 選取您的「應用程式類型」。在這個範例中,應用程式類型為網頁應用程式
  5. 按照下列方式註冊應用程式可存取 Google API 的來源:

    1. 若要識別用戶端 ID,請輸入名稱。
    2. 在「Authorized JavaScript origins」(已授權的 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 驗證程式庫。

您的應用程式必須通過 Google 審查,才能發布用於存取 Library API 的公開應用程式。測試應用程式時,畫面上會顯示「未驗證的應用程式」訊息,直到應用程式通過驗證為止。

設定用戶端程式庫

PHP 用戶端程式庫會為您處理所有後端 API 呼叫,並提供適合使用的物件,包括一些常見 API 工作的程式碼範例。首先,從 GitHub 下載並安裝 PHP 適用的 Google Photos Library API 用戶端程式庫。接下來,為 PHP 設定 OAuth2 憑證。

下載選項

使用 composer,在開發環境中加入程式庫做為依附元件。執行下列指令,將程式庫新增至專案設定,並下載至 vendor/ 目錄。

composer require google/photos-library

您也可以複製存放區下載壓縮的 tarball

為 PHP 設定 OAuth2 憑證

此用戶端程式庫支援 PHP 適用的 Google 驗證程式庫。詳情請參閱「將 OAuth 2.0 與 PHP 適用的 Google API 用戶端程式庫搭配使用」一文。

設定 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 探索更多範例