ตรวจสอบสิทธิ์โดยใช้ Yahoo และ C++
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณอนุญาตให้ผู้ใช้ตรวจสอบสิทธิ์กับ Firebase ได้โดยใช้ผู้ให้บริการ OAuth เช่น
Yahoo ด้วยการผสานรวมการเข้าสู่ระบบ OAuth ทั่วไปบนเว็บเข้ากับแอปของคุณโดยใช้
Firebase SDK เพื่อดำเนินการลงชื่อเข้าใช้ตั้งแต่ต้นจนจบ เนื่องจากขั้นตอนนี้ต้องใช้
การใช้ Firebase SDK บนโทรศัพท์
จะรองรับเฉพาะบน Android และ
แพลตฟอร์มของ Apple
ก่อนเริ่มต้น
- เพิ่ม Firebase ไปยังโปรเจ็กต์ C++
- ในคอนโซล Firebase ให้เปิดส่วน Auth
- ในแท็บวิธีการลงชื่อเข้าใช้ ให้เปิดใช้ผู้ให้บริการ Yahoo
- เพิ่มรหัสไคลเอ็นต์และรหัสลับไคลเอ็นต์จาก Play Console ของผู้ให้บริการรายนั้นลงใน
การกำหนดค่าผู้ให้บริการ:
-
หากต้องการลงทะเบียนไคลเอ็นต์ Yahoo OAuth ให้ทำตามนักพัฒนาซอฟต์แวร์ของ Yahoo
เอกสารประกอบเกี่ยวกับ
การลงทะเบียนเว็บแอปพลิเคชันกับ Yahoo
โปรดเลือกสิทธิ์ 2 อย่างของ OpenID Connect API ดังนี้
profile
และ email
- เมื่อลงทะเบียนแอปกับผู้ให้บริการเหล่านี้ อย่าลืมลงทะเบียน
โดเมน
*.firebaseapp.com
สำหรับโปรเจ็กต์ของคุณเป็นโดเมนการเปลี่ยนเส้นทางสำหรับ
แอป
- คลิกบันทึก
เข้าถึงชั้นเรียน firebase::auth::Auth
คลาส
Auth
เป็นเกตเวย์สำหรับการเรียก API ทั้งหมด
- เพิ่มไฟล์ส่วนหัว Auth และ App ดังนี้
#include "firebase/app.h"
#include "firebase/auth.h"
- ในโค้ดเริ่มต้น ให้สร้าง
firebase::App
#if defined(__ANDROID__)
firebase::App* app =
firebase::App::Create(firebase::AppOptions(), my_jni_env, my_activity);
#else
firebase::App* app = firebase::App::Create(firebase::AppOptions());
#endif // defined(__ANDROID__)
- รับชั้นเรียน
firebase::auth::Auth
สำหรับ firebase::App
มีการแมปแบบหนึ่งต่อหนึ่งระหว่าง App
และ Auth
firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
จัดการขั้นตอนการลงชื่อเข้าใช้ด้วย Firebase SDK
หากต้องการจัดการขั้นตอนการลงชื่อเข้าใช้ด้วย Firebase SDK ให้ทำตามขั้นตอนต่อไปนี้
สร้างอินสแตนซ์ของ FederatedOAuthProviderData
ที่กำหนดค่าด้วย
รหัสผู้ให้บริการที่เหมาะกับ Yahoo!
firebase::auth::FederatedOAuthProviderData
provider_data(firebase::auth::YahooAuthProvider::kProviderId);
ไม่บังคับ: ระบุพารามิเตอร์ OAuth ที่กำหนดเองเพิ่มเติมที่ต้องการ
ส่งด้วยคำขอ OAuth
// Prompt user to re-authenticate to Yahoo.
provider_data.custom_parameters["prompt"] = "login";
// Localize to French.
provider_data.custom_parameters["language"] = "fr";
สำหรับพารามิเตอร์ที่ Yahoo รองรับ โปรดดูที่
เอกสาร OAuth ของ Yahoo
โปรดทราบว่าคุณไม่สามารถส่งพารามิเตอร์ที่จําเป็นของ Firebase ด้วย
custom_parameters()
พารามิเตอร์เหล่านี้คือ client_id
redirect_uri, response_type, scope และ state
ไม่บังคับ: ระบุขอบเขต OAuth 2.0 เพิ่มเติมนอกเหนือจาก profile
และ
email
ที่ต้องการขอจากผู้ให้บริการตรวจสอบสิทธิ์ หาก
แอปพลิเคชันต้องการเข้าถึงข้อมูลส่วนตัวของผู้ใช้จาก Yahoo APIs คุณจะ
ต้องขอสิทธิ์ไปยัง Yahoo APIs ในสิทธิ์ API ใน
Yahoo Developer Console ขอบเขต OAuth ที่ขอต้องตรงกันทุกประการกับ
รายการที่กำหนดไว้ล่วงหน้าในสิทธิ์ API ของแอป เช่น หากอ่าน/เขียน
มีการขอสิทธิ์เข้าถึงรายชื่อติดต่อของผู้ใช้และกำหนดค่าล่วงหน้าใน API ของแอป
ต้องส่งผ่าน sdct-w
แทนขอบเขต OAuth แบบอ่านอย่างเดียว
sdct-r
มิฉะนั้น ขั้นตอนนี้จะล้มเหลวและเกิดข้อผิดพลาด
ผู้ใช้ปลายทาง
// Request access to Yahoo Mail API.
provider_data.scopes.push_back("mail-r");
// This must be preconfigured in the app's API permissions.
provider_data.scopes.push_back("sdct-w");
ดูข้อมูลเพิ่มเติมได้ที่
เอกสารประกอบเกี่ยวกับขอบเขต Yahoo
เมื่อกำหนดค่าข้อมูลผู้ให้บริการแล้ว ให้ใช้ข้อมูลเพื่อสร้าง
FederatedOAuthProvider
// Construct a FederatedOAuthProvider for use in Auth methods.
firebase::auth::FederatedOAuthProvider provider(provider_data);
ตรวจสอบสิทธิ์กับ Firebase โดยใช้ออบเจ็กต์ผู้ให้บริการการตรวจสอบสิทธิ์ โปรดทราบว่าสิ่งที่
การดำเนินการ FirebaseAuth อื่นๆ ขั้นตอนนี้จะควบคุม UI ของคุณด้วยการ
เป็นมุมมองเว็บที่ผู้ใช้สามารถป้อนข้อมูลเข้าสู่ระบบได้
หากต้องการเริ่มขั้นตอนการลงชื่อเข้าใช้ โปรดโทรหา SignInWithProvider
firebase::Future<firebase::auth::AuthResult> result =
auth->SignInWithProvider(provider_data);
จากนั้น แอปพลิเคชันของคุณอาจต้องรอหรือ
ให้ลงทะเบียนการโทรกลับในอนาคต
แม้ว่าตัวอย่างข้างต้นจะเน้นที่ขั้นตอนการลงชื่อเข้าใช้ แต่คุณก็มี
ความสามารถในการลิงก์ผู้ให้บริการ Yahoo กับผู้ใช้ที่มีอยู่โดยใช้
LinkWithProvider
ตัวอย่างเช่น คุณสามารถลิงก์
เป็นผู้ให้บริการรายเดียวกัน โดยอนุญาตให้ลงชื่อเข้าใช้ด้วยผู้ให้บริการใดบริการหนึ่งได้
firebase::Future<firebase::auth::AuthResult> result = user.LinkWithProvider(provider_data);
รูปแบบเดียวกันนี้สามารถใช้กับ ReauthenticateWithProvider
ซึ่งสามารถ
ใช้เพื่อเรียกข้อมูลข้อมูลเข้าสู่ระบบใหม่สำหรับการดำเนินการที่มีความละเอียดอ่อนซึ่งจำเป็นต้องใช้
การเข้าสู่ระบบครั้งล่าสุด
firebase::Future<firebase::auth::AuthResult> result =
user.ReauthenticateWithProvider(provider_data);
จากนั้นแอปพลิเคชันของคุณอาจรอหรือลงทะเบียนการโทรกลับในวันที่
อนาคต
ขั้นสูง: จัดการขั้นตอนการลงชื่อเข้าใช้ด้วยตนเอง
ซึ่งต่างจากผู้ให้บริการ OAuth รายอื่นๆ ที่ Firebase รองรับ เช่น Google, Facebook
และ Twitter ซึ่งสามารถลงชื่อเข้าใช้ด้วยโทเค็นเพื่อการเข้าถึง OAuth ได้โดยตรง
ตามข้อมูลเข้าสู่ระบบ Firebase Auth ไม่สนับสนุนความสามารถเดียวกันนี้สำหรับ
ของผู้ให้บริการอย่าง Yahoo เนื่องจากไม่สามารถ
เซิร์ฟเวอร์การตรวจสอบสิทธิ์เพื่อยืนยันกลุ่มเป้าหมายของโทเค็นเพื่อการเข้าถึงของ Yahoo OAuth
ซึ่งเป็นข้อกำหนดด้านความปลอดภัยที่สำคัญมาก และอาจทำให้แอปพลิเคชัน
เพื่อแสดงการโจมตีซ้ำซึ่งได้รับโทเค็นเพื่อการเข้าถึง Yahoo OAuth สำหรับ
โปรเจ็กต์หนึ่ง (ผู้โจมตี) สามารถใช้เพื่อลงชื่อเข้าใช้โปรเจ็กต์อื่น (เหยื่อ) ได้
แต่ Firebase Auth ให้คุณจัดการขั้นตอน OAuth ทั้งหมดและ
การแลกเปลี่ยนรหัสการให้สิทธิ์โดยใช้รหัสไคลเอ็นต์และข้อมูลลับของ OAuth
ซึ่งกำหนดค่าไว้ในคอนโซล Firebase เนื่องจากรหัสการให้สิทธิ์สามารถใช้ได้เฉพาะ
ร่วมกับรหัสไคลเอ็นต์/ข้อมูลลับเฉพาะ ซึ่งก็คือรหัสการให้สิทธิ์
ที่ได้รับสำหรับโครงการหนึ่ง จะไม่สามารถนำไปใช้กับอีกโครงการหนึ่งได้
หากจำเป็นต้องใช้ผู้ให้บริการเหล่านี้ในสภาพแวดล้อมที่ไม่รองรับ
ไลบรารี OAuth ของบุคคลที่สามและ
การตรวจสอบสิทธิ์ที่กำหนดเองของ Firebase
จะใช้งาน วิธีแรกคือการตรวจสอบสิทธิ์กับผู้ให้บริการ
และเพื่อแลกเปลี่ยนข้อมูลเข้าสู่ระบบของผู้ให้บริการกับโทเค็นที่กำหนดเอง
ขั้นตอนถัดไป
หลังจากผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก ระบบจะสร้างบัญชีผู้ใช้ใหม่ และ
ซึ่งก็คือชื่อผู้ใช้และรหัสผ่าน โทรศัพท์
หมายเลข หรือข้อมูลของผู้ให้บริการตรวจสอบสิทธิ์ ซึ่งก็คือผู้ใช้ที่ลงชื่อเข้าใช้ ใหม่นี้
จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และสามารถใช้เพื่อระบุ
ผู้ใช้สำหรับทุกแอปในโปรเจ็กต์ของคุณ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใดก็ตาม
-
ในแอป คุณสามารถดูข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ได้จาก
firebase::auth::User
ออบเจ็กต์:
firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
std::string name = user.display_name();
std::string email = user.email();
std::string photo_url = user.photo_url();
// The user's ID, unique to the Firebase project.
// Do NOT use this value to authenticate with your backend server,
// if you have one. Use firebase::auth::User::Token() instead.
std::string uid = user.uid();
}
ในFirebase Realtime DatabaseและCloud Storage
กฎความปลอดภัย คุณสามารถทำสิ่งต่อไปนี้
รับรหัสผู้ใช้ที่ไม่ซ้ำของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth
และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้
คุณอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปโดยใช้การตรวจสอบสิทธิ์หลายรายการได้
โดยลิงก์ข้อมูลเข้าสู่ระบบของผู้ให้บริการการตรวจสอบสิทธิ์กับ
บัญชีผู้ใช้ที่มีอยู่เดิม
หากต้องการนำผู้ใช้ออกจากระบบ โปรดโทร
SignOut()
auth->SignOut();
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-29 UTC
[null,null,["อัปเดตล่าสุด 2025-08-29 UTC"],[],[],null,["You can let your users authenticate with Firebase using OAuth providers like\nYahoo by integrating web-based generic OAuth Login into your app using the\nFirebase SDK to carry out the end to end sign-in flow. Since this flow requires\nthe use of the phone-based Firebase SDKs, it is only supported on Android and\nApple platforms.\n\nBefore you begin\n\n1. [Add Firebase to your C++ project](/docs/cpp/setup#note_select_platform).\n2. In the [Firebase console](//console.firebase.google.com/), open the **Auth** section.\n3. On the **Sign in method** tab, enable the **Yahoo** provider.\n4. Add the **Client ID** and **Client Secret** from that provider's developer console to the provider configuration:\n 1. To register a Yahoo OAuth client, follow the Yahoo developer\n documentation on [registering a web application with Yahoo](https://developer.yahoo.com/oauth2/guide/openid_connect/getting_started.html).\n\n Be sure to select the two OpenID Connect API permissions:\n `profile` and `email`.\n 2. When registering apps with these providers, be sure to register the `*.firebaseapp.com` domain for your project as the redirect domain for your app.\n5. Click **Save**.\n\nAccess the `firebase::auth::Auth` class The `Auth` class is the gateway for all API calls.\n\n1. Add the Auth and App header files: \n\n ```c++\n #include \"firebase/app.h\"\n #include \"firebase/auth.h\"\n ```\n2. In your initialization code, create a [`firebase::App`](/docs/reference/cpp/class/firebase/app) class. \n\n ```c++\n #if defined(__ANDROID__)\n firebase::App* app =\n firebase::App::Create(firebase::AppOptions(), my_jni_env, my_activity);\n #else\n firebase::App* app = firebase::App::Create(firebase::AppOptions());\n #endif // defined(__ANDROID__)\n ```\n3. Acquire the `firebase::auth::Auth` class for your `firebase::App`. There is a one-to-one mapping between `App` and `Auth`. \n\n ```c++\n firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);\n ```\n\nHandle the sign-in flow with the Firebase SDK\n\nTo handle the sign-in flow with the Firebase SDK, follow these steps:\n\n1. Construct an instance of a `FederatedOAuthProviderData` configured with\n the provider ID appropriate for Yahoo.\n\n firebase::auth::FederatedOAuthProviderData\n provider_data(firebase::auth::YahooAuthProvider::kProviderId);\n\n2. **Optional**: Specify additional custom OAuth parameters that you want to\n send with the OAuth request.\n\n // Prompt user to re-authenticate to Yahoo.\n provider_data.custom_parameters[\"prompt\"] = \"login\";\n\n // Localize to French.\n provider_data.custom_parameters[\"language\"] = \"fr\";\n\n For the parameters Yahoo supports, see the\n [Yahoo OAuth documentation](https://developer.yahoo.com/oauth2/guide/openid_connect/getting_started.html).\n Note that you can't pass Firebase-required parameters with\n `custom_parameters()`. These parameters are **client_id** ,\n **redirect_uri** , **response_type** , **scope** and **state**.\n3. **Optional** : Specify additional OAuth 2.0 scopes beyond `profile` and\n `email` that you want to request from the authentication provider. If your\n application requires access to private user data from Yahoo APIs, you'll\n need to request permissions to Yahoo APIs under **API Permissions** in the\n Yahoo developer console. Requested OAuth scopes must be exact matches to the\n preconfigured ones in the app's API permissions. For example if, read/write\n access is requested to user contacts and preconfigured in the app's API\n permissions, `sdct-w` has to be passed instead of the readonly OAuth scope\n `sdct-r`. Otherwise,the flow will fail and an error would be shown to the\n end user.\n\n // Request access to Yahoo Mail API.\n provider_data.scopes.push_back(\"mail-r\");\n // This must be preconfigured in the app's API permissions.\n provider_data.scopes.push_back(\"sdct-w\");\n\n To learn more, refer to the\n [Yahoo scopes documentation](https://developer.yahoo.com/oauth2/guide/yahoo_scopes/).\n4. Once your provider data has been configured, use it to create a\n FederatedOAuthProvider.\n\n // Construct a FederatedOAuthProvider for use in Auth methods.\n firebase::auth::FederatedOAuthProvider provider(provider_data);\n\n5. Authenticate with Firebase using the Auth provider object. Note that unlike\n other FirebaseAuth operations, this will take control of your UI by popping\n up a web view in which the user can enter their credentials.\n\n To start the sign in flow, call `SignInWithProvider`: \n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result =\n auth-\u003eSignInWithProvider(provider_data);\n\n Your application may then wait or [register a callback on the Future](#register_callback_on_future).\n6. While the above examples focus on sign-in flows, you also have the\n ability to link a Yahoo provider to an existing user using\n `LinkWithProvider`. For example, you can link multiple\n providers to the same user allowing them to sign in with either.\n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result = user.LinkWithProvider(provider_data);\n\n7. The same pattern can be used with `ReauthenticateWithProvider` which can be\n used to retrieve fresh credentials for sensitive operations that require\n recent login.\n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result =\n user.ReauthenticateWithProvider(provider_data);\n\n Your application may then wait or [register a callback on\n the Future](#register_callback_on_future).\n\nAdvanced: Handle the sign-in flow manually\n\nUnlike other OAuth providers supported by Firebase such as Google, Facebook,\nand Twitter, where sign-in can directly be achieved with OAuth access token\nbased credentials, Firebase Auth does not support the same capability for\nproviders such as Yahoo due to the inability of the Firebase\nAuth server to verify the audience of Yahoo OAuth access tokens.\nThis is a critical security requirement and could expose applications and\nwebsites to replay attacks where a Yahoo OAuth access token obtained for\none project (attacker) can be used to sign in to another project (victim).\nInstead, Firebase Auth offers the ability to handle the entire OAuth flow and\nthe authorization code exchange using the OAuth client ID and secret\nconfigured in the Firebase Console. As the authorization code can only be used\nin conjunction with a specific client ID/secret, an authorization code\nobtained for one project cannot be used with another.\n\nIf these providers are required to be used in unsupported environments, a\nthird party OAuth library and\n[Firebase custom authentication](../admin/create-custom-tokens)\nwould need to be used. The former is needed to authenticate with the provider\nand the latter to exchange the provider's credential for a custom token.\n\nNext steps\n\nAfter a user signs in for the first time, a new user account is created and\nlinked to the credentials---that is, the user name and password, phone\nnumber, or auth provider information---the user signed in with. This new\naccount is stored as part of your Firebase project, and can be used to identify\na user across every app in your project, regardless of how the user signs in.\n\n- In your apps, you can get the user's basic profile information from the\n [`firebase::auth::User`](/docs/reference/cpp/class/firebase/auth/user) object:\n\n ```c++\n firebase::auth::User user = auth-\u003ecurrent_user();\n if (user.is_valid()) {\n std::string name = user.display_name();\n std::string email = user.email();\n std::string photo_url = user.photo_url();\n // The user's ID, unique to the Firebase project.\n // Do NOT use this value to authenticate with your backend server,\n // if you have one. Use firebase::auth::User::Token() instead.\n std::string uid = user.uid();\n }\n ```\n- In your Firebase Realtime Database and Cloud Storage\n [Security Rules](/docs/database/security/user-security), you can\n get the signed-in user's unique user ID from the `auth` variable,\n and use it to control what data a user can access.\n\nYou can allow users to sign in to your app using multiple authentication\nproviders by [linking auth provider credentials to an\nexisting user account.](/docs/auth/cpp/account-linking)\n\nTo sign out a user, call [`SignOut()`](/docs/reference/cpp/class/firebase/auth/auth#signout): \n\n```c++\nauth-\u003eSignOut();\n```"]]