尝试使用此Google客户端API示例
https://developers.google.com/identity/sign-in/web/backend-auth
我通过javascript获取访问令牌并通过ajax javascript对象将其发布到我的PHP。
expires_at
:
1490552860336
expires_in
:
2223
first_issued_at
:
1490549260336
id_token
:
"eyJhbGciOiJSUzI1NiIsImtpZCI6ImRkMDM4NzVmMzkzMTdiZThlNTc0MDBlNmMzMDYzMmFmNTU4YzJkZTQifQ.eyJhenAiOiI5Mjg3MzEzODM5NzQtc3RsdmFyc291cnZoNmdqaDdvbjkwZWwxOGhjOHVrNXYuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiI5Mjg3MzEzODM5NzQtc3RsdmFyc291cnZoNmdqaDdvbjkwZWwxOGhjOHVrNXYuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMTQ1MTMzNjU2ODMwMzExNzMwOTgiLCJlbWFpbCI6IndoeXRlYXZlbWVjaGFuaWNhbEBnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXRfaGFzaCI6IkF2SUZQaHNjVmFNY1htTEdtand6M2ciLCJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiaWF0IjoxNDkwNTQ5MjU2LCJleHAiOjE0OTA1NTI4NTYsIm5hbWUiOiJUcmF2aXMgS2xlaW4iLCJwaWN0dXJlIjoiaHR0cHM6Ly9saDQuZ29vZ2xldXNlcmNvbnRlbnQuY29tLy1BdzlCUjF4YnVBNC9BQUFBQUFBQUFBSS9BQUFBQUFBQUFBQS9BTWNBWWk5b2ZNV1NtcllGc2pTc0xUcFpZZHhkT0FEaGZBL3M5Ni1jL3Bob3RvLmpwZyIsImdpdmVuX25hbWUiOiJUcmF2aXMiLCJmYW1pbHlfbmFtZSI6IktsZWluIiwibG9jYWxlIjoiZW4ifQ.PBcvfMs9_MO9mcHDtGGyb3VbO-O374tHH7w4gWr8ZMpRDYyynY6ZQd5BxRxlh6b__upzxz6gMYQ29yY__zDnEp4_NrkSp7RWQAcoF-JuY8xM1GtDy5IwEFKPxDh9h-9bGoxCx5jgX7-bu2F5Xk6opwZUzoyyYk5BT1xXpBfAe35CJHK_NAzSfrwEX8vU-hIP9C_BjipXptg5Eo3fi3DxPWRVbrzsscV0DDASm9xEi5ilLkUxBwRZN9bk6hgXXgn2Ts7Mo4B7Rue1oyB_rkNJ3RSgH7ylnHrVePiVwJ_15aKwiD64FgcmdLOruLEm9pADoiAodMVNZ6HAElr3MuWG1w"
idpId
:
"google"
login_hint
:
"AJDLj6JimYVJadfBfXai38izEPbdRaSSCtY5FyzYIVQ1IPE2kad13fq5-9Qwkik2AlWx3_Qlco531-R7LVEwcxvs8Xhw0TvrSg"
session_state
:
Object
token_type
:
"Bearer"
我的PHP如下
require_once 'google-api-php-client-2.1.1/vendor/autoload.php';
// Get $id_token via HTTPS POST.
$client = new Google_Client(['client_id' =>'my google client id' ]);
$payload = $client->verifyIdToken($this->input->post('id_token'));
if ($payload) {
$userid = $payload['sub'];
// If request specified a G Suite domain:
//$domain = $payload['hd'];
} else {
// Invalid ID token
}
令人沮丧的是,我从来没有幸运过谷歌API示例。我似乎无法在网上找到任何可以让它在这些日子里工作的东西。请帮我。我基本上都在寻找可以验证并保存在我的数据库中的ID或令牌
我被困在这个功能上
$payload = $client->verifyIdToken($this->input->post('id_token'));
屏幕截图显示了在抛出错误之前我得到了多远。
我最终走了这条路线
$q = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $this->input->post('id_token');
$json = file_get_contents($q);
$userInfoArray = json_decode($json,true);
$googleEmail = $userInfoArray['email'];
$google_id= $userInfoArray['kid'];
我不确定该孩子是否是该用户的唯一ID,但它不断返回相同的值。
答案 0 :(得分:0)
感谢, 我的
<?php
require_once 'google-api-php-client-2.2.0_PHP54/vendor/autoload.php';
// Get $id_token via HTTPS POST.
$id_token= $_POST['id_token'];
$CLIENT_ID = 'yourclientid......apps.googleusercontent.com';
$client = new Google_Client(['client_id' => $CLIENT_ID]);
$payload = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $id_token;
$json = file_get_contents($payload);
$userInfoArray = json_decode($json,true);
$googleEmail = $userInfoArray['email'];
$google_id= $userInfoArray['sub'];
if ($googleEmail)
{
echo "validated".$googleEmail;
} else {
echo "inValidate" ;
}
?>