1

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.

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 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.

Thank you very much.

2 Answers 2

6

if you are not using google client library you can use this google api For getting the client details you just pass the token to this 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>";

This will authenticate the user and it will return object

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
)

fount an example here : http://wiki.workassis.com/implementing-google-sign-in-for-websites

Sign up to request clarification or add additional context in comments.

Comments

2

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";

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

4 Comments

At first thank you. But where is $plus variable from? It is instance of what? I am not a witch.
I have found this code: $plus = new \Google_PlusService($client); but it throws exception: Class 'Google_PlusService' not found. I have instaled Google api via Composer: require: {"google/apiclient": "1.0.*@beta"}
It is insane!!! It seems the right form is $plus = new \Google_Service_Plus($client);
Sorry, I must have missed a couple of lines copying the code. I've updated the question with what should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.