หากคุณต้องการดึงเฉพาะรหัสผู้ใช้ Google ชื่อและรูปภาพสำหรับผู้เยี่ยมชมเว็บแอปของคุณนี่คือโซลูชันด้านบริการ PHP ที่แท้จริงของฉันสำหรับปี 2020 โดยไม่มีไลบรารีภายนอกใช้ -
หากคุณอ่านคู่มือการใช้ OAuth 2.0 สำหรับเว็บเซิร์ฟเวอร์แอปพลิเคชันโดย Google (และระวัง Google ชอบเปลี่ยนลิงก์ไปยังเอกสารของตัวเอง) คุณจะต้องดำเนินการเพียง 2 ขั้นตอนเท่านั้น:
- นำเสนอหน้าเว็บแก่ผู้เข้าชมเพื่อขอความยินยอมในการแบ่งปันชื่อของเธอกับเว็บแอปของคุณ
 
- จากนั้นนำ "รหัส" ที่ส่งผ่านหน้าเว็บด้านบนไปยังแอปพลิเคชันเว็บของคุณและดึงโทเค็น (จริงๆแล้ว 2) จาก Google
 
หนึ่งในโทเค็นที่ส่งคืนเรียกว่า "id_token" และมีรหัสผู้ใช้ชื่อและรูปถ่ายของผู้เยี่ยมชม
นี่คือโค้ด PHP ของเกมบนเว็บโดยฉัน ตอนแรกฉันใช้ Javascript SDK แต่แล้วฉันสังเกตเห็นว่าข้อมูลผู้ใช้ปลอมสามารถส่งผ่านไปยังเกมบนเว็บของฉันได้เมื่อใช้ SDK ฝั่งไคลเอ็นต์เท่านั้น (โดยเฉพาะรหัสผู้ใช้ซึ่งสำคัญสำหรับเกมของฉัน) ดังนั้นฉันจึงเปลี่ยนไปใช้ PHP ทางฝั่งเซิร์ฟเวอร์:
<?php
const APP_ID       = '1234567890-abcdefghijklmnop.apps.googleusercontent.com';
const APP_SECRET   = 'abcdefghijklmnopq';
const REDIRECT_URI = 'https://the/url/of/this/PHP/script/';
const LOCATION     = 'Location: https://accounts.google.com/o/oauth2/v2/auth?';
const TOKEN_URL    = 'https://oauth2.googleapis.com/token';
const ERROR        = 'error';
const CODE         = 'code';
const STATE        = 'state';
const ID_TOKEN     = 'id_token';
# use a "random" string based on the current date as protection against CSRF
$CSRF_PROTECTION   = md5(date('m.d.y'));
if (isset($_REQUEST[ERROR]) && $_REQUEST[ERROR]) {
    exit($_REQUEST[ERROR]);
}
if (isset($_REQUEST[CODE]) && $_REQUEST[CODE] && $CSRF_PROTECTION == $_REQUEST[STATE]) {
    $tokenRequest = [
        'code'          => $_REQUEST[CODE],
        'client_id'     => APP_ID,
        'client_secret' => APP_SECRET,
        'redirect_uri'  => REDIRECT_URI,
        'grant_type'    => 'authorization_code',
    ];
    $postContext = stream_context_create([
        'http' => [
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($tokenRequest)
        ]
    ]);
    # Step #2: send POST request to token URL and decode the returned JWT id_token
    $tokenResult = json_decode(file_get_contents(TOKEN_URL, false, $postContext), true);
    error_log(print_r($tokenResult, true));
    $id_token    = $tokenResult[ID_TOKEN];
    # Beware - the following code does not verify the JWT signature! 
    $userResult  = json_decode(base64_decode(str_replace('_', '/', str_replace('-', '+', explode('.', $id_token)[1]))), true);
    $user_id     = $userResult['sub'];
    $given_name  = $userResult['given_name'];
    $family_name = $userResult['family_name'];
    $photo       = $userResult['picture'];
    if ($user_id != NULL && $given_name != NULL) {
        # print your web app or game here, based on $user_id etc.
        exit();
    }
}
$userConsent = [
    'client_id'     => APP_ID,
    'redirect_uri'  => REDIRECT_URI,
    'response_type' => 'code',
    'scope'         => 'profile',
    'state'         => $CSRF_PROTECTION,
];
# Step #1: redirect user to a the Google page asking for user consent
header(LOCATION . http_build_query($userConsent));
?>
คุณสามารถใช้ไลบรารี PHP เพื่อเพิ่มความปลอดภัยเพิ่มเติมได้โดยการยืนยันลายเซ็น JWT สำหรับวัตถุประสงค์ของฉันมันไม่จำเป็นเพราะฉันเชื่อว่า Google จะไม่ทรยศต่อเกมบนเว็บเล็ก ๆ ของฉันด้วยการส่งข้อมูลผู้เยี่ยมชมปลอม
นอกจากนี้หากคุณต้องการรับข้อมูลส่วนบุคคลเพิ่มเติมของผู้เยี่ยมชมคุณต้องมีขั้นตอนที่สาม:
const USER_INFO    = 'https://www.googleapis.com/oauth2/v3/userinfo?access_token=';
const ACCESS_TOKEN = 'access_token'; 
# Step #3: send GET request to user info URL
$access_token = $tokenResult[ACCESS_TOKEN];
$userResult = json_decode(file_get_contents(USER_INFO . $access_token), true);
หรือคุณอาจจะได้รับสิทธิ์เพิ่มเติมในนามของผู้ใช้ - ดูรายการยาวที่OAuth 2.0 ขอบเขตสำหรับ Google APIs doc
สุดท้ายค่าคงที่ APP_ID และ APP_SECRET ที่ใช้ในโค้ดของฉันคุณจะได้รับจากคอนโซล Google API :
