通过Javascript和PHP登录Google+ [英] Google+ login via Javascript and PHP

查看:146
本文介绍了通过Javascript和PHP登录Google+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can you tell me please, how can I get user data(name, email) via Google+ Javascript/Php api? I go according this tutorial, which describes how to get authResult['code'] via Javascript.

没关系,我知道了.我通过Ajax将其发送到服务器,但我不知道如何通过PHP BUT获取用户名和电子邮件之类的用户数据,而无需任何重定向.这是代码示例 Javascript:

Thats ok I have it. I send it to server via Ajax but I don't know how to get user data like name and email via PHP BUT without any redirects. Here are the samples of code 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在每个页面上所述进行任何重定向.您能帮我吗,我找不到任何解决方案.

PHP retrieves an access_token but no users data. Ofcourse it is an ajax call so I can't do any redirects as Google describes on every page. Can you help me please I can't find any solution.

非常感谢您.

推荐答案

一旦您拥有该用户的经过身份验证的客户端,您便希望向

Once you have an authenticated client for the user you will want to make a request to the 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";

对于他们的电子邮件,您必须进行迭代在$me['emails']数组上,并使用type=account查找值.

For their email you will have to iterate over the $me['emails'] array and find the value with type=account.

这篇关于通过Javascript和PHP登录Google+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆