您目前查看的是 Google Photos Library API 的舊版說明文件。
開始使用 PHP 用戶端程式庫
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要開始使用 Google Photos Library API 搭配 PHP 用戶端程式庫,你必須
在您的開發環境中設定用戶端程式庫。
在新增專案前,請先啟用 API 設定專案
,並設定 OAuth 2.0 用戶端 ID。
您的應用程式代表
Google 相簿使用者。例如,當您在使用者的
Google 相簿相片庫或將媒體項目上傳到使用者的
Google 相簿帳戶,使用者透過
OAuth 2.0 通訊協定。
OAuth 2.0 用戶端 ID 可讓應用程式使用者登入、驗證
然後使用 Library APILibrary API 不支援
或服務帳戶如要使用這個 API,使用者必須登入有效的 Google
。
啟用 API
您必須先為專案啟用 Library API,才能使用該 API。
- 前往 Google API 控制台。
- 在選單列中,選取專案或建立新專案。
- 如要開啟 Google API 程式庫,請從導覽選單選取
API 與服務 >程式庫。
- 搜尋「Google Photos Library API」。選取正確的結果並按一下
啟用:
要求 OAuth 2.0 用戶端 ID
請按照下列步驟申請 OAuth 用戶端 ID,並為其進行設定
應用程式。這個範例使用的應用程式,其可存取整個 OAuth 流程
,如我們範例中所示設定程序可能不同
其他導入情境。
- 前往 Google API 控制台
然後選取所需專案
- 從選單中選取「API 與」服務 >憑證。
- 在「Credentials」(憑證) 頁面上,按一下「Create Credentials」(建立憑證) > OAuth 用戶端 ID。
- 選取您的「應用程式類型」。在這個範例中,應用程式類型為
網頁應用程式。
註冊應用程式可存取 Google API 的來源
如下所示:
- 為識別用戶端 ID,請輸入名稱。
在「授權的 JavaScript 來源」欄位中,輸入
應用程式。這個欄位不允許使用萬用字元。
您可以輸入多個來源,讓應用程式在不同的位置執行
通訊協定、網域或子網域您輸入的網址可以
開始 OAuth 要求。
以下範例是本機開發網址 (我們的範例使用
localhost:8080
) 和正式版網址。
http://localhost:8080
https://myproductionurl.example.com
「授權的重新導向 URI」欄位是接收到的端點
OAuth 2.0 伺服器的回應。通常這包括
並指向應用程式中的路徑
http://localhost:8080/auth/google/callback
https://myproductionurl.example.com/auth/google/callback
按一下「建立」。
-
在產生的 OAuth 用戶端對話方塊中,下載 JSON 檔案
包含用戶端設定用戶端詳細資料包含
包括:
這個 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 驗證程式庫
PHP。如需更多資訊
請參閱將 OAuth 2.0 與 Google API 用戶端程式庫搭配使用,
PHP。
進行設定時,使用驗證程式庫傳回的驗證憑證
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 上的 samples
讓你自行探索。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間:2025-08-29 (世界標準時間)。"],[[["\u003cp\u003eTo use the Google Photos Library API with PHP, enable the API, request an OAuth 2.0 client ID, and set up the PHP client library.\u003c/p\u003e\n"],["\u003cp\u003eUsers must sign in with a Google Account as the API does not support service accounts, requiring OAuth 2.0 for authorization.\u003c/p\u003e\n"],["\u003cp\u003eSet up your OAuth2 credentials by configuring a web application in Google API Console with authorized origins and redirect URIs.\u003c/p\u003e\n"],["\u003cp\u003eDownload and install the Google Photos Library API client library for PHP using composer or by cloning the repository.\u003c/p\u003e\n"],["\u003cp\u003eAuthenticate users using the Google Auth Library for PHP, then utilize the PhotosLibraryClient to interact with the API and access its features.\u003c/p\u003e\n"]]],["To use the Google Photos Library API with PHP, first, enable the API in the Google API Console and create an OAuth 2.0 client ID, specifying your app's authorized JavaScript origins and redirect URIs. Download the client ID's JSON file, containing the client ID and secret. Next, install the PHP client library via Composer, and set up OAuth2 credentials using the downloaded JSON file. Use these credentials to set up the `PhotosLibraryClient` and try API calls.\n"],null,["# Get started with PHP client library\n\nTo start using the Google Photos Library API with the PHP client library, you will need to\n[set up the client library](#get-library) in your development environment.\nBefore you do that, [configure your project](#configure-app) by enabling the API\nvia the Google API Console and setting up an OAuth 2.0 client ID.\n\nYour application interacts with Google Photos on behalf of a\nGoogle Photos user. For instance, when you create albums in a user's\nGoogle Photos library or upload media items to a user's\nGoogle Photos account, the user authorizes these API requests via the\n[OAuth 2.0](/identity/protocols/OAuth2) protocol.\n\nThe OAuth 2.0 client ID allows your application users to sign in, authenticate,\nand thereby use the Library API. The Library API does not support\nservice accounts; to use this API, users must be signed in to a valid Google\nAccount.\n\nConfigure your app\n------------------\n\n### Enable the API\n\nBefore you can use the Library API, you must enable it for your project.\n\n1. Go to the [Google API Console](https://console.developers.google.com/apis/library).\n2. From the menu bar, select a project or create a new project.\n3. To open the Google API Library, from the Navigation menu, select **APIs \\& Services \\\u003e Library**.\n4. Search for \"Google Photos Library API\". Select the correct result and click **Enable**.\n\n### Request an OAuth 2.0 client ID\n\nFollow the steps below to request an OAuth client ID and configure it for your\napplication. This example uses an application where the entire OAuth flow is\nhandled server-side, such as the one in our samples. The setup process may vary\nfor other [implementation scenarios](/identity/protocols/OAuth2).\n\n1. Go to the [Google API Console](https://console.developers.google.com/apis/library) and select your project.\n2. From the menu, select **APIs \\& Services \\\u003e Credentials**.\n3. On the **Credentials** page, click **Create Credentials \\\u003e OAuth client ID**.\n4. Select your **Application type** . In this example, the application type is **Web application**.\n5. Register the origins from which your app is allowed to access the Google APIs\n as follows:\n\n 1. To identify the client ID, enter a name.\n 2. In the **Authorized JavaScript origins** field, enter the origin for your\n app. This field doesn't allow wildcards.\n\n You can enter multiple origins to allow your app to run on different\n protocols, domains, or subdomains. The URLs you enter are allowed to\n start an OAuth request.\n\n The following example shows a local development URL (our samples use\n `localhost:8080`) and a production URL. \n\n http://localhost:8080\n https://myproductionurl.example.com\n\n 3. The **Authorized redirect URI** field is the endpoint that receives\n responses from the OAuth 2.0 server. Typically, this includes your\n development environment and points to a path in your application.\n\n http://localhost:8080/auth/google/callback\n https://myproductionurl.example.com/auth/google/callback\n\n 4. Click **Create**.\n\n\u003c!-- --\u003e\n\n6. From the resulting OAuth client dialog, download the JSON file\n containing your client configuration. You client details consist of the\n following:\n\n - Client ID\n - Client secret\n\n This JSON file will be used later to [set up](#get-library)\n the Google Auth library for PHP which works with this client library.\n\nBefore you can launch a public application that accesses the Library API,\nyour app must be reviewed by Google. An \"Unverified app\" message appears on the\nscreen when you test your application, until it is\n[verified](https://support.google.com/cloud/answer/7454865).\n\nSet up the client library\n-------------------------\n\nThe PHP client library handles all the backend API calls for you, and exposes\nfriendly objects to work with, including code samples for some common API tasks.\nFirstly, download and install the Google Photos Library API client library for PHP along with\nthe dependencies from [GitHub](https://github.com/google/php-photoslibrary).\nThen, set up your OAuth2 credentials for PHP.\n\n### Download options\n\nUse [composer](https://github.com/google/php-photoslibrary#using-composer-require)\nto include the library as a dependency in your development environment. Run the\nfollowing command to add the library to your project configuration and download\nit to the `vendor/` directory. \n\n composer require google/photos-library\n\nAlternatively, you can also\n[clone the\nrepository](https://github.com/google/php-photoslibrary#using-git-clone) or\n[download a compressed\ntarball](https://github.com/google/php-photoslibrary#downloading-a-compressed-tarball).\n\n### Set up your OAuth2 credentials for PHP\n\nThis client library works with the\n[Google Auth Library for\nPHP](https://github.com/google/google-auth-library-php). For more information,\nrefer to [Using OAuth 2.0 with the Google API Client Library for\nPHP](https://developers.google.com/api-client-library/php/auth/web-app).\n\nUse the authentication credentials returned by the auth library when setting up\nthe `PhotosLibraryClient`.\n\nTry out some samples\n--------------------\n\nTry the code below to make your first API call using the PHP client library. \n\n use Google\\Auth\\Credentials\\UserRefreshCredentials;\n use Google\\Photos\\Library\\V1\\PhotosLibraryClient;\n use Google\\Photos\\Library\\V1\\PhotosLibraryResourceFactory;\n\n try {\n // Use the OAuth flow provided by the Google API Client Auth library\n // to authenticate users. See the file /src/common/common.php in the samples for a complete\n // authentication example.\n $authCredentials = new UserRefreshCredentials( /* Add your scope, client secret and refresh token here */ );\n\n // Set up the Photos Library Client that interacts with the API\n $photosLibraryClient = new PhotosLibraryClient(['credentials' =\u003e $authCredentials]);\n\n // Create a new Album object with at title\n $newAlbum = PhotosLibraryResourceFactory::album(\"My Album\");\n\n // Make the call to the Library API to create the new album\n $createdAlbum = $photosLibraryClient-\u003ecreateAlbum($newAlbum);\n\n // The creation call returns the ID of the new album\n $albumId = $createdAlbum-\u003egetId();\n } catch (\\Google\\ApiCore\\ApiException $exception) {\n // Error during album creation\n } catch (\\Google\\ApiCore\\ValidationException $e) {\n // Error during client creation\n echo $exception;\n }\n\nThere are more\n[samples](https://github.com/google/php-photoslibrary/tree/samples) on GitHub\nfor you to try."]]