Google+通过Javascript和PHP登录

时间:2015-07-04 00:25:06

标签: php google-plus google-oauth google-login

请告诉我,如何通过Google+ Javascript / Php api获取用户数据(姓名,电子邮件)?我按照tutorial进行操作,其中介绍了如何通过Javascript获取authResult['code']

多数民众赞成我拥有它。我通过Ajax将它发送到服务器,但我不知道如何通过PHP BUT获取用户数据,如名称和电子邮件,没有任何重定向。以下是代码示例 使用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在每个页面上描述的那样进行任何重定向。能帮到我吗,我找不到任何解决办法。

非常感谢。

2 个答案:

答案 0 :(得分:4)

如果您不使用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>";

这将验证用户,它将返回对象

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

答案 1 :(得分:2)

为用户提供经过身份验证的客户端后,您需要向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找到该值。