Android Google登录验证ID令牌

时间:2018-08-30 19:01:57

标签: php google-signin google-api-client

所以我在android应用程序中实现了Google登录,现在我想在php后端上对登录用户进行身份验证。我遵循了本教程:https://developers.google.com/identity/sign-in/android/backend-auth。我做了页面中的所有内容,然后添加了客户端ID。 (来自Google API控制台的客户端ID,名称为Web客户端(为Google登录自动创建))。但是,当我尝试测试后端时,出现以下错误:

Uncaught GuzzleHttp\Exception\ConnectException:
cURL error 7: Failed to connect to www.googleapis.com port 443:
Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
in /srv/disk1/***********/google-api-
client/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:185

我在做什么错了?

这是我的php代码:

<?php
include('database.php');
require_once 'google-api-client/vendor/autoload.php';

/////////////////////////////////

$request_type = $_SERVER['REQUEST_METHOD'];
$CLIENT_ID = "***";

if ($request_type === 'POST') {
    $entityBody = file_get_contents('php://input');
    $temp = json_decode($entityBody);
    $data = json_decode(json_encode($temp), true);
    if ($data['id_token']) {
        $client = new Google_Client(['client_id' => $CLIENT_ID]);
        $access_token = $client->getAccessToken();
        $payload = $client->verifyIdToken($data['id_token']);
        if ($payload) {
            header('Content-Type: application/json');
            $query = query($connection, 
                'SELECT * FROM `*****`', 
                []);
            print(json_encode($query));  
        } else {
            http_response_code(401);
            print("Unauthorized request.");
        }
    }
    else {
        http_response_code(401);
        print("No id token received.");
    } 
}
else {
    http_response_code(405);
    print("Method not allowed.");
}
?>

0 个答案:

没有答案
相关问题