PHP构建JWT签名

// Google Oauth2.0 JWT
function JWT() {
    $time = time();
    $header = ['alg' => 'RS256', 'typ' => 'JWT'];
    $claims = [
        'iss' => 'app@01.iam.gserviceaccount.com', 
        'scope' => 'https://www.googleapis.com/auth/photoslibrary',
        'aud' => 'https://www.googleapis.com/oauth2/v4/token', 
        'exp' => $time + 3600,
        'iat' => $time
    ];
    $msg = Base64URLEncode(json_encode($header)) . '.' . Base64URLEncode(json_encode($claims));
    $privateKey = "-----BEGIN PRIVATE KEY-----\nKEY\n-----END PRIVATE KEY-----\n";
    $sign = '';
    openssl_sign($msg, $sign, $privateKey, 'SHA256');
    return $msg . '.' . Base64URLEncode($sign);
}

function Base64URLEncode($input) {
    return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
}

echo JWT();