คุณกำลังดูเอกสารประกอบเดิมสำหรับ Google Photos Library API
เริ่มต้นใช้งานไลบรารีของไคลเอ็นต์ Java
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ในการเริ่มใช้ Google Photos Library API กับไลบรารีของไคลเอ็นต์ Java คุณจะต้องดำเนินการดังนี้
ตั้งค่าไลบรารีของไคลเอ็นต์ในสภาพแวดล้อมการพัฒนาของคุณ
แต่ก่อนที่จะดำเนินการดังกล่าว ให้กำหนดค่าโปรเจ็กต์โดยเปิดใช้ API
ผ่านคอนโซล Google API และตั้งค่ารหัสไคลเอ็นต์ OAuth 2.0
แอปพลิเคชันของคุณโต้ตอบกับ Google Photos ในนามของ
ผู้ใช้ Google Photos เช่น เมื่อคุณสร้างอัลบั้มใน
คลังภาพ Google Photos หรืออัปโหลดรายการสื่อไปยัง
บัญชี Google Photos ให้ผู้ใช้ให้สิทธิ์คำขอ API เหล่านี้ผ่าน
โปรโตคอล OAuth 2.0
รหัสไคลเอ็นต์ OAuth 2.0 ช่วยให้ผู้ใช้แอปพลิเคชันลงชื่อเข้าใช้ ตรวจสอบสิทธิ์
และนั่นก็คือการใช้ Library API Library API ไม่รองรับ
บัญชีบริการ หากต้องการใช้ API นี้ ผู้ใช้ต้องลงชื่อเข้าใช้บัญชี
บัญชี
เปิดใช้ API
ก่อนที่จะใช้ Library API ได้ คุณต้องเปิดใช้ API สำหรับโปรเจ็กต์ก่อน
- ไปที่คอนโซล Google API
- จากแถบเมนู ให้เลือกโปรเจ็กต์หรือสร้างโปรเจ็กต์ใหม่
- หากต้องการเปิดไลบรารี Google API จากเมนูการนำทาง ให้เลือก
API และ บริการ > คลัง
- ค้นหา "Google Photos Library API" เลือกผลลัพธ์ที่ถูกต้องและคลิก
เปิดใช้
ขอรหัสไคลเอ็นต์ OAuth 2.0
ทําตามขั้นตอนด้านล่างเพื่อขอรหัสไคลเอ็นต์ OAuth และกําหนดค่าให้กับ
แอปพลิเคชัน ตัวอย่างนี้ใช้แอปพลิเคชันที่ขั้นตอน OAuth ทั้งหมด
ฝั่งเซิร์ฟเวอร์ที่จัดการ อย่างเช่นในตัวอย่างของเรา ขั้นตอนการตั้งค่าอาจแตกต่างกันไป
เพื่อดูสถานการณ์การใช้งานอื่นๆ
- ไปที่คอนโซล Google API
แล้วเลือกโปรเจ็กต์
- จากเมนู ให้เลือก API & บริการ > ข้อมูลเข้าสู่ระบบ
- ในหน้าข้อมูลเข้าสู่ระบบ ให้คลิกสร้างข้อมูลเข้าสู่ระบบ > รหัสไคลเอ็นต์ OAuth
- เลือกประเภทแอปพลิเคชัน ในตัวอย่างนี้ ประเภทแอปพลิเคชันคือ
เว็บแอปพลิเคชัน
ลงทะเบียนต้นทางที่แอปของคุณได้รับอนุญาตให้เข้าถึง Google APIs
ดังนี้
- หากต้องการระบุรหัสไคลเอ็นต์ ให้ป้อนชื่อ
ในช่องต้นทาง JavaScript ที่ได้รับอนุญาต ให้ป้อนต้นทาง
แอป ช่องนี้ไม่อนุญาตให้ใช้ไวลด์การ์ด
คุณป้อนต้นทางหลายแห่งได้เพื่อให้แอปทำงานบน
โปรโตคอล โดเมน หรือโดเมนย่อย URL ที่คุณป้อนได้รับอนุญาตให้
เริ่มคำขอ OAuth
ตัวอย่างต่อไปนี้แสดง URL การพัฒนาในเครื่อง (ตัวอย่างของเราใช้
localhost:8080
) และ URL เวอร์ชันที่ใช้งานจริง
http://localhost:8080
https://myproductionurl.example.com
ช่อง URL การเปลี่ยนเส้นทางที่ได้รับอนุญาตคือปลายทางที่รับ
การตอบกลับจากเซิร์ฟเวอร์ OAuth 2.0 โดยปกติแล้วจะรวมถึง
การพัฒนาซอฟต์แวร์และชี้ไปยังเส้นทางในแอปพลิเคชันของคุณ
http://localhost:8080/auth/google/callback
https://myproductionurl.example.com/auth/google/callback
คลิกสร้าง
-
ดาวน์โหลดไฟล์ JSON จากกล่องโต้ตอบไคลเอ็นต์ OAuth ที่แสดง
ที่มีการกำหนดค่าไคลเอ็นต์ของคุณ รายละเอียดไคลเอ็นต์ของคุณประกอบด้วย
ดังต่อไปนี้:
- Client-ID
- รหัสลับไคลเอ็นต์
ระบบจะใช้ไฟล์ JSON นี้เพื่อตั้งค่าในภายหลัง
ไลบรารีการตรวจสอบสิทธิ์ของ Google สำหรับ Java ซึ่งทำงานร่วมกับไลบรารีของไคลเอ็นต์นี้
ก่อนที่คุณจะสามารถเปิดแอปพลิเคชันสาธารณะที่เข้าถึง Library API ได้
แอปของคุณต้องได้รับการตรวจสอบจาก Google "แอปที่ไม่ได้รับการยืนยัน" ปรากฏใน
เมื่อคุณทดสอบแอปพลิเคชัน จนกว่าจะ
ได้รับการยืนยันแล้ว
ตั้งค่าไลบรารีไคลเอ็นต์
ไลบรารีของไคลเอ็นต์ Java จะจัดการการเรียก API แบ็กเอนด์ทั้งหมดให้คุณ และจะแสดง
ออบเจ็กต์ friendly ที่ต้องทำงานด้วย ซึ่งรวมถึงตัวอย่างโค้ดสำหรับงาน API ทั่วไปบางรายการ
ขั้นแรก ให้ดาวน์โหลดและติดตั้งไลบรารีของไคลเอ็นต์ Google Photos Library API สำหรับ Java
ด้วยทรัพยากร Dependency จาก GitHub
จากนั้นตั้งค่าข้อมูลเข้าสู่ระบบ OAuth2 สำหรับ Java
ตัวเลือกการดาวน์โหลด
ตัวเลือกในการดาวน์โหลดไลบรารีของไคลเอ็นต์มีดังนี้
ทรัพยากร Dependency ของ Gradle:
หากต้องการใช้ไลบรารีนี้กับ Gradle ให้เพิ่ม Dependency ต่อไปนี้
build.gradle
ไฟล์
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.photos.library:google-photos-library-client:1.7.3'
}
การขึ้นต่อกันของ Maven:
หากต้องการใช้ไลบรารีนี้กับ Maven ให้เพิ่มรายการต่อไปนี้ลงใน Maven ของคุณ pom.xml
<dependency>
<groupId>com.google.photos.library</groupId>
<artifactId>google-photos-library-client</artifactId>
<version>1.7.3</version>
</dependency>
วิธีดาวน์โหลดรุ่น
หน้าผลงาน
มีอาร์ติแฟกต์ที่แตกต่างกันสำหรับไลบรารีแต่ละรุ่น รวมถึงไฟล์ jar
โคลนที่เก็บด้วยคำสั่งต่อไปนี้
ใช้วิธีนี้หากต้องการเปลี่ยนแปลงหรือมีส่วนร่วมในไลบรารีนี้
เช่น การส่งคำขอดึง หรือหากต้องการลองใช้ตัวอย่างของเรา เมื่อคุณ
โคลนที่เก็บ ระบบจะดาวน์โหลดไฟล์ทั้งหมดในที่เก็บนี้
- เรียกใช้
git clone https://github.com/google/java-photoslibrary.git
ที่
Command Prompt
- คุณจะได้รับไดเรกทอรี
java-photoslibrary
ไปที่หน้าดังกล่าวโดยเรียกใช้
cd java-photoslibrary
- เปิดไฟล์
build.gradle
ใน IDE หรือเรียกใช้ ./gradlew assemble
ที่
พร้อมท์คำสั่งเพื่อสร้างโปรเจ็กต์ ดู ./gradlew tasks
งานที่มีอยู่
ตั้งค่าข้อมูลเข้าสู่ระบบ OAuth2 สำหรับ Java
ไลบรารีของไคลเอ็นต์นี้ใช้งานได้กับไลบรารีการตรวจสอบสิทธิ์ของ Google สำหรับ
Java สำหรับข้อมูลเพิ่มเติม
โปรดดูที่การใช้ OAuth 2.0 กับไลบรารีของไคลเอ็นต์ Google API สำหรับ
Java
ระบุการกำหนดค่า OAuth ของไคลเอ็นต์ใน CredentialsProvider
เมื่อ
สร้าง PhotoLibrarySettings
สำหรับออบเจ็กต์ PhotosLibraryClient
ลองดูตัวอย่าง
ลองใช้โค้ดด้านล่างเพื่อเรียก API ครั้งแรกโดยใช้ไลบรารีของไคลเอ็นต์ Java
// Set up the Photos Library Client that interacts with the API
PhotosLibrarySettings settings =
PhotosLibrarySettings.newBuilder()
.setCredentialsProvider(
FixedCredentialsProvider.create(/* Add credentials here. */))
.build();
try (PhotosLibraryClient photosLibraryClient =
PhotosLibraryClient.initialize(settings)) {
// Create a new Album with at title
Album createdAlbum = photosLibraryClient.createAlbum("My Album");
// Get some properties from the album, such as its ID and product URL
String id = album.getId();
String url = album.getProductUrl();
} catch (ApiException e) {
// Error during album creation
}
มีตัวอย่างเพิ่มเติม
ใน GitHub เพื่อให้คุณลองใช้
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-29 UTC
[null,null,["อัปเดตล่าสุด 2025-08-29 UTC"],[[["\u003cp\u003eThe Google Photos Library API allows your application to interact with Google Photos on behalf of a user, requiring user authorization through OAuth 2.0.\u003c/p\u003e\n"],["\u003cp\u003eBefore using the API, you must enable it in the Google API Console and obtain an OAuth 2.0 client ID for your application.\u003c/p\u003e\n"],["\u003cp\u003eThe Java client library simplifies API interaction, and you can install it using Gradle, Maven, or by downloading a release from GitHub.\u003c/p\u003e\n"],["\u003cp\u003eTo authenticate your application, set up OAuth2 credentials using the Google Auth Library for Java and specify your client configuration in the \u003ccode\u003eCredentialsProvider\u003c/code\u003e.\u003c/p\u003e\n"]]],["To use the Google Photos Library API with Java, first enable the API in the Google API Console and create an OAuth 2.0 client ID, configuring authorized JavaScript origins and redirect URIs. Next, download the Java client library using Gradle, Maven, a direct download, or by cloning the GitHub repository. Finally, set up OAuth2 credentials using the Google Auth Library for Java, providing these in the `PhotosLibrarySettings` when initializing the `PhotosLibraryClient`. Then try out some samples.\n"],null,["# Get started with Java client library\n\nTo start using the Google Photos Library API with the Java 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 Java 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 Java 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 Java along\nwith the dependencies from [GitHub](https://github.com/google/java-photoslibrary).\nThen, set up your OAuth2 credentials for Java.\n\n### Download options\n\nHere are some options to download the client library:\n\n- **Gradle dependency:**\n\n To use this library with Gradle, add the following dependency to your\n `build.gradle` file. \n\n repositories {\n mavenCentral()\n }\n dependencies {\n compile 'com.google.photos.library:google-photos-library-client:1.7.3'\n }\n\n- **Maven dependency:**\n\n To use this library with Maven, add the following to your Maven `pom.xml`\n file. \n\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.photos.library\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-photos-library-client\u003c/artifactId\u003e\n \u003cversion\u003e1.7.3\u003c/version\u003e\n \u003c/dependency\u003e\n\n- **Download a release:**\n\n The [releases page](https://github.com/google/java-photoslibrary/releases)\n contains different artifacts for each library release, including jar files.\n- **Clone the repository:**\n\n Use this method if you want to alter or contribute to this library, for\n example, submitting pull requests, or if you wish to try our samples. When you\n clone the repository, **all** files in this repository will be downloaded.\n 1. Run `git clone https://github.com/google/java-photoslibrary.git` at the command prompt.\n 2. You'll get a `java-photoslibrary` directory. Navigate to it by running `cd java-photoslibrary`.\n 3. Open the `build.gradle` file in your IDE or run `./gradlew assemble` at the command prompt to build the project. See `./gradlew tasks` to see available tasks.\n\n### Set up your OAuth2 credentials for Java\n\nThis client library works with the [Google Auth Library for\nJava](https://github.com/google/google-auth-library-java). For more information,\nrefer to [Using OAuth 2.0 with the Google API Client Library for\nJava](https://developers.google.com/api-client-library/java/google-api-java-client/oauth2).\n\nSpecify your client OAuth configuration in the `CredentialsProvider` when\ncreating the `PhotoLibrarySettings` for a `PhotosLibraryClient` object.\n\nTry out some samples\n--------------------\n\nTry the code below to make your first API call using the Java client library. \n\n // Set up the Photos Library Client that interacts with the API\n PhotosLibrarySettings settings =\n PhotosLibrarySettings.newBuilder()\n .setCredentialsProvider(\n FixedCredentialsProvider.create(/* Add credentials here. */)) \n .build();\n\n try (PhotosLibraryClient photosLibraryClient =\n PhotosLibraryClient.initialize(settings)) {\n\n // Create a new Album with at title\n Album createdAlbum = photosLibraryClient.createAlbum(\"My Album\");\n\n // Get some properties from the album, such as its ID and product URL\n String id = album.getId();\n String url = album.getProductUrl();\n\n } catch (ApiException e) {\n // Error during album creation\n }\n\nThere are more [samples](https://github.com/google/java-photoslibrary/tree/master/sample)\non GitHub for you to try."]]