Google+ 通过 Javascript 和 PHP 登录
Google+ login via Javascript and PHP
你能告诉我,我如何通过Google+ Javascript/Php api 获取用户数据(姓名,电子邮件)?我根据这个 tutorial,它描述了如何通过 Javascript 获得 authResult['code']
。
没关系,我有。我通过 Ajax 将它发送到服务器,但我不知道如何通过 PHP 获取用户数据,如姓名和电子邮件,但没有任何重定向。这是代码示例
Javascript:
function googleStart()
{
console.log('googleStart');
gapi.load('auth2', function()
{
auth2 = gapi.auth2.init({
client_id: '811214467813-v3fmui5dfghte5m0kmohsf6dl11ori3tg.apps.googleusercontent.com',
// Scopes to request in addition to 'profile' and 'email'
fetch_basic_profile: false,
scope: 'profile email'
});
});
}
function googleSignIn(authResult)
{
console.log(authResult);
if (authResult['code']) {
console.log(authResult);
// Hide the sign-in button now that the user is authorized, for example:
$('#signinButton').attr('style', 'display: none');
// Send the code to the server
$.ajax({
type: 'POST',
url : "{link :Signgoogle:in}",
accepts : 'json',
// Spýtať sa na DJPW čo to tu je zakomentované
//contentType: 'application/octet-stream; charset=utf-8',
//processData: false,
data : {
'code' : authResult['code'],
'id_token' : authResult.getAuthResponse().id_token
},
...
PHP:
public function actionIn()
{
$code = $_POST['code'];
$client = new \Google_Client();
$client->setClientId(self::APP_ID);
$client->setClientSecret(self::APP_SECRET);
$client->setRedirectUri(self::REDIRECT_URI);
$client->authenticate($code);
$access_token = $client->getAccessToken();
$client->setAccessToken($access_token);
$client->getAuth();
$data = $access_token;
if($data)
{
$this->sendJson(array($data));
}
else
{
$data = array('error' => '$client->verifyIdToken($id) skočil chybou.');
$this->sendJson($data);
}
}
....
PHP 检索 access_token
但没有用户数据。当然,这是一个 ajax 调用,所以我不能像 Google 在每个页面上描述的那样进行任何重定向。你能帮我吗我找不到任何解决办法。
非常感谢。
一旦您拥有用户的经过身份验证的客户端,您将希望向 Google+ people.get API method.
发出请求
$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setAccessToken($access_token);
$plus = new Google_Service_Plus($client);
$me = $plus->people->get('me');
print "ID: {$me['id']}\n";
print "Display Name: {$me['displayName']}\n";
print "Image Url: {$me['image']['url']}\n";
print "Url: {$me['url']}\n";
对于他们的 email,您将必须遍历 $me['emails']
数组并找到 type=account
的值。
如果您不使用 google 客户端库,您可以使用此 google api 要获取客户端详细信息,您只需将令牌传递给此 api https://www.googleapis.com/oauth2/v3/tokeninfo?id_token= < your_token >
$response = file_get_contents ( 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $_REQUEST['token'] );
$response = json_decode ( $response );
echo "<pre>";
print_r ( $response );
echo "</pre>";
这将对用户进行身份验证并将return对象
stdClass Object
(
[iss] => accounts.google.com
[at_hash] => hE0R********nxg
[aud] => 79*********************p.apps.googleusercontent.com
[sub] => 1028*****2
[email_verified] => true
[azp] => 7945823****p.apps.googleusercontent.com
[email] => bik***@gmail.com
[iat] => 14***7
[exp] => 1***07
[name] => Bik****M
[picture] => https://lh6.googleusercontent.com/-W8cA***/photo.jpg
[given_name] => Bike***
[family_name] => M
[locale] => en
[alg] => RS256
[kid] => 70f6c4267*******cb
)
在此处创建示例:http://wiki.workassis.com/implementing-google-sign-in-for-websites
你能告诉我,我如何通过Google+ Javascript/Php api 获取用户数据(姓名,电子邮件)?我根据这个 tutorial,它描述了如何通过 Javascript 获得 authResult['code']
。
没关系,我有。我通过 Ajax 将它发送到服务器,但我不知道如何通过 PHP 获取用户数据,如姓名和电子邮件,但没有任何重定向。这是代码示例 Javascript:
function googleStart()
{
console.log('googleStart');
gapi.load('auth2', function()
{
auth2 = gapi.auth2.init({
client_id: '811214467813-v3fmui5dfghte5m0kmohsf6dl11ori3tg.apps.googleusercontent.com',
// Scopes to request in addition to 'profile' and 'email'
fetch_basic_profile: false,
scope: 'profile email'
});
});
}
function googleSignIn(authResult)
{
console.log(authResult);
if (authResult['code']) {
console.log(authResult);
// Hide the sign-in button now that the user is authorized, for example:
$('#signinButton').attr('style', 'display: none');
// Send the code to the server
$.ajax({
type: 'POST',
url : "{link :Signgoogle:in}",
accepts : 'json',
// Spýtať sa na DJPW čo to tu je zakomentované
//contentType: 'application/octet-stream; charset=utf-8',
//processData: false,
data : {
'code' : authResult['code'],
'id_token' : authResult.getAuthResponse().id_token
},
...
PHP:
public function actionIn()
{
$code = $_POST['code'];
$client = new \Google_Client();
$client->setClientId(self::APP_ID);
$client->setClientSecret(self::APP_SECRET);
$client->setRedirectUri(self::REDIRECT_URI);
$client->authenticate($code);
$access_token = $client->getAccessToken();
$client->setAccessToken($access_token);
$client->getAuth();
$data = $access_token;
if($data)
{
$this->sendJson(array($data));
}
else
{
$data = array('error' => '$client->verifyIdToken($id) skočil chybou.');
$this->sendJson($data);
}
}
....
PHP 检索 access_token
但没有用户数据。当然,这是一个 ajax 调用,所以我不能像 Google 在每个页面上描述的那样进行任何重定向。你能帮我吗我找不到任何解决办法。
非常感谢。
一旦您拥有用户的经过身份验证的客户端,您将希望向 Google+ people.get API method.
发出请求$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setAccessToken($access_token);
$plus = new Google_Service_Plus($client);
$me = $plus->people->get('me');
print "ID: {$me['id']}\n";
print "Display Name: {$me['displayName']}\n";
print "Image Url: {$me['image']['url']}\n";
print "Url: {$me['url']}\n";
对于他们的 email,您将必须遍历 $me['emails']
数组并找到 type=account
的值。
如果您不使用 google 客户端库,您可以使用此 google api 要获取客户端详细信息,您只需将令牌传递给此 api https://www.googleapis.com/oauth2/v3/tokeninfo?id_token= < your_token >
$response = file_get_contents ( 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $_REQUEST['token'] );
$response = json_decode ( $response );
echo "<pre>";
print_r ( $response );
echo "</pre>";
这将对用户进行身份验证并将return对象
stdClass Object
(
[iss] => accounts.google.com
[at_hash] => hE0R********nxg
[aud] => 79*********************p.apps.googleusercontent.com
[sub] => 1028*****2
[email_verified] => true
[azp] => 7945823****p.apps.googleusercontent.com
[email] => bik***@gmail.com
[iat] => 14***7
[exp] => 1***07
[name] => Bik****M
[picture] => https://lh6.googleusercontent.com/-W8cA***/photo.jpg
[given_name] => Bike***
[family_name] => M
[locale] => en
[alg] => RS256
[kid] => 70f6c4267*******cb
)
在此处创建示例:http://wiki.workassis.com/implementing-google-sign-in-for-websites