Platform
ลงทะเบียนด้วย Google
9 นาที
การลงชื่อเข้าใช้ด้วย google tutorial บทนำ การลงชื่อเข้าใช้ด้วย google ช่วยให้ผู้ใช้สามารถลงชื่อเข้าใช้แอปด้วยบัญชี google ของตนได้ ข้อกำหนดเบื้องต้น ในการทำตามบทเรียนนี้ คุณจะต้องมี แอปที่สร้างขึ้นที่ back4app ดูที่ https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอปที่ back4app ตั้งค่า subdomain สำหรับแอป back4app ของคุณ ดูที่ https //www back4app com/docs/platform/activating web hosting เพื่อเรียนรู้วิธีการสร้าง subdomain ใน back4app บัญชี https //developers google com/?hl=pt br 1 สร้างแอป back4app ใหม่ ก่อนอื่น คุณต้องมั่นใจว่าคุณมีแอปที่สร้างขึ้นที่ back4app แล้ว อย่างไรก็ตาม หากคุณเป็นผู้ใช้ใหม่ คุณสามารถตรวจสอบ https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอปหนึ่ง 2 สร้าง client identifier ใหม่ เข้าสู่ระบบบัญชี https //developers google com/ ของคุณและไปที่ google api console google api console คลิกที่ credentials credentials และเลือก oauth 2 0 client ids oauth 2 0 client ids หากคุณไม่มี consent screen google จะขอให้คุณสร้างหนึ่ง คลิกที่ configure consent screen configure consent screen , คุณจะถูกเปลี่ยนเส้นทางไปยังหน้าต่อไปนี้ กรอกการตั้งค่าความยินยอมของหน้าจอให้ครบถ้วนและกด บันทึก บันทึก เลือกแพลตฟอร์มที่คุณต้องการ สำหรับตัวอย่างนี้ ฉันใช้ javascript (web application) แต่คุณควรเลือกแพลตฟอร์มที่คุณจะใช้ ใน authorized javascript origins authorized javascript origins , เปลี่ยน url เป็นซับโดเมนของคุณ ใน authorized redirect uris authorized redirect uris , ใส่ซับโดเมนของคุณตามด้วย /redirect /redirect ตามที่แสดงในภาพด้านล่าง หมายเหตุ หากคุณยังไม่ได้เปิดใช้งานซับโดเมนของคุณ โปรดตรวจสอบคู่มือนี้เพื่อดูว่าคุณจะทำได้อย่างไร https //www back4app com/docs/platform/activating web hosting หลังจากนั้นคุณควรมี client id และ secret ของคุณ 3 ดึงโค้ดของคุณ ไปที่ url ต่อไปนี้ โดยเปลี่ยนค่าของ redirect uri redirect uri และ client id client id เป็นค่าที่คุณสร้างขึ้น 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 ต่อไปนี้โดยแทนที่ค่า your code your code , client id client id , client secret client secret , และ redirect uri redirect uri สำหรับค่าของแอปพลิเคชันของคุณ 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 ของคุณได้ จำไว้ว่า รหัสสามารถใช้ได้เพียงครั้งเดียว หากคุณได้รับข้อผิดพลาดหรือไม่ใช้โทเค็นของคุณ คุณต้องสร้างรหัสใหม่เพื่อให้สามารถรันได้อีกครั้ง ตอนนี้ถึงเวลาที่จะดึง รหัสผู้ใช้ของ google รหัสผู้ใช้ของ google มันเป็นสตริงตัวเลขที่คุณจะส่งต่อเป็น id id ในขั้นตอนที่ 4。 เพื่อทำเช่นนั้น ให้รันคำสั่งต่อไปนี้โดยแทนที่ your token your token ด้วยโทเค็นที่คุณได้รับในคำสั่งก่อนหน้า 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 } จำไว้ว่านี่ต้องทำในทุกการเข้าสู่ระบบสำหรับผู้ใช้ทุกคน