ลงทะเบียนด้วย Google
9 นาที
การลงชื่อเข้าใช้ด้วย google tutorial บทนำ การลงชื่อเข้าใช้ด้วย google ช่วยให้ผู้ใช้สามารถลงชื่อเข้าใช้แอปด้วยบัญชี google ของตนได้ ข้อกำหนดเบื้องต้น ในการทำตามบทเรียนนี้ คุณจะต้องมี แอปที่สร้างขึ้นที่ back4app ดูที่ บทเรียนการสร้างแอปใหม่ https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอปที่ back4app ตั้งค่า subdomain สำหรับแอป back4app ของคุณ ดูที่ การเปิดใช้งาน web hosting และ live query https //www back4app com/docs/platform/activating web hosting เพื่อเรียนรู้วิธีการสร้าง subdomain ใน back4app บัญชี google developer https //developers google com/?hl=pt br 1 สร้างแอป back4app ใหม่ ก่อนอื่น คุณต้องมั่นใจว่าคุณมีแอปที่สร้างขึ้นที่ back4app แล้ว อย่างไรก็ตาม หากคุณเป็นผู้ใช้ใหม่ คุณสามารถตรวจสอบ บทแนะนำนี้ https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอปหนึ่ง 2 สร้าง client identifier ใหม่ เข้าสู่ระบบบัญชี google developer https //developers google com/ ของคุณและไปที่ \<font color="#2166ae">google api console\</font> คลิกที่ \<font color="#2166ae">credentials\</font> และเลือก \<font color="#2166ae">oauth 2 0 client ids\</font> หากคุณไม่มี consent screen google จะขอให้คุณสร้างหนึ่ง คลิกที่ \<font color="#2166ae">configure consent screen\</font> , คุณจะถูกเปลี่ยนเส้นทางไปยังหน้าต่อไปนี้ กรอกการตั้งค่าความยินยอมของหน้าจอให้ครบถ้วนและกด \<font color="#2166ae">บันทึก\</font> เลือกแพลตฟอร์มที่คุณต้องการ สำหรับตัวอย่างนี้ ฉันใช้ javascript (web application) แต่คุณควรเลือกแพลตฟอร์มที่คุณจะใช้ ใน \<font color="#2166ae">authorized javascript origins\</font> , เปลี่ยน url เป็นซับโดเมนของคุณ ใน \<font color="#2166ae">authorized redirect uris\</font> , ใส่ซับโดเมนของคุณตามด้วย \<font color="#2166ae">/redirect\</font> ตามที่แสดงในภาพด้านล่าง หมายเหตุ หากคุณยังไม่ได้เปิดใช้งานซับโดเมนของคุณ โปรดตรวจสอบคู่มือนี้เพื่อดูว่าคุณจะทำได้อย่างไร สร้างซับโดเมนของคุณ https //www back4app com/docs/platform/activating web hosting หลังจากนั้นคุณควรมี client id และ secret ของคุณ 3 ดึงโค้ดของคุณ ไปที่ url ต่อไปนี้ โดยเปลี่ยนค่าของ \<font color="#2166ae">redirect uri\</font> และ \<font color="#2166ae">client id\</font> เป็นค่าที่คุณสร้างขึ้น https //accounts google com/o/oauth2/v2/auth?scope=https%3a//www googleapis com/auth/drive metadata readonly\&access type=offline\&include granted scopes=true\&response type=code\&state=state parameter passthrough value\&redirect uri=redirect url\&client id=client id ขอบเขตที่จำเป็นในการดึง auth token และต่อมา user id คือ https //www googleapis com/auth/userinfo email https //www googleapis com/auth/plus me https //www googleapis com/auth/userinfo profile เข้าสู่ระบบด้วยบัญชี google ของคุณและเว็บไซต์ที่ถูกเปลี่ยนเส้นทางจะมีรหัสของคุณใน url คัดลอกส่วนของรหัสใน url เท่านั้นและรันคำสั่ง curl ต่อไปนี้โดยแทนที่ค่า \<font color="#2166ae">your code\</font> , \<font color="#2166ae">client id\</font> , \<font color="#2166ae">client secret\</font> , และ \<font color="#2166ae">redirect uri\</font> สำหรับค่าของแอปพลิเคชันของคุณ 1 curl x post \\ 2 https //oauth2 googleapis com/token \\ 3 f 'grant type=authorization code' \\ 4 f 'code=your code' \\ 5 f 'client id=client id' \\ 6 f 'client secret=client secret' \\ 7 f 'redirect uri=redirect uri' รันมันและคุณควรจะดึง access token ของคุณได้ จำไว้ว่า รหัสสามารถใช้ได้เพียงครั้งเดียว หากคุณได้รับข้อผิดพลาดหรือไม่ใช้โทเค็นของคุณ คุณต้องสร้างรหัสใหม่เพื่อให้สามารถรันได้อีกครั้ง ตอนนี้ถึงเวลาที่จะดึง \<font color="#2166ae">รหัสผู้ใช้ของ google\</font> มันเป็นสตริงตัวเลขที่คุณจะส่งต่อเป็น \<font color="#2166ae">id\</font> ในขั้นตอนที่ 4。 เพื่อทำเช่นนั้น ให้รันคำสั่งต่อไปนี้โดยแทนที่ \<font color="#2166ae">your token\</font> ด้วยโทเค็นที่คุณได้รับในคำสั่งก่อนหน้า 1 curl x get https //www googleapis com/userinfo/v2/me?access token=your token 4 เริ่มการพัฒนา ตอนนี้ที่การลงชื่อเข้าใช้ด้วย google ได้ถูกกำหนดค่าแล้ว คุณสามารถเริ่มกระบวนการพัฒนาได้。 รูปแบบสำหรับ authdata คือ 1 { 2 "google" { 3 "id" "user's google id (string)", 4 "id token" "an authorized google id token for the user (use when not using access token)", 5 "access token" "an authorized google access token for the user (use when not using id token)" 6 } 7 } นี่คือวิธีการสำหรับ ios sdk 1 pfuser loginwithauthtype(inbackground "google", authdata \["access token"\ tokenstring, "id" user]) continuewith { task > any? in 2 3 } และนี่คือสำหรับ android sdk 1 map\<string, string> authdata = new hashmap\<string, string>(); 2 authdata put("access token", tokenstring); 3 authdata put("id", user); 4 parseuser loginwithinbackground("google", authdata){ 5 6 } จำไว้ว่านี่ต้องทำในทุกการเข้าสู่ระบบสำหรับผู้ใช้ทุกคน