简体   繁体   中英

Google+ javascript/php login api

Can you help me please how to login by G+ Javascript and PHP? Javascript works fine, but when I want to verify the token on server it throws an exceptions. I go according this page: https://developers.google.com/identity/sign-in/web/backend-auth but there is only Java/Python. Javscript code:

function onSignIn(googleUser)
{
    var id_token = googleUser.getAuthResponse().id_token,
        profile = googleUser.getBasicProfile();

    console.log(id_token);

    $.ajax({
        url : "{link :Signgoogle:in}",
        accepts : 'json',
        type : 'post',
        data : {
            'token' : id_token
        },
...

PHP code:

public function actionIn()
{
    $token = $_POST['token'];

    $client = new \Google_Client();
    $client->setScopes('email');
    $client->setApplicationName(self::APP_NAME);
    $client->setDeveloperKey(self::SERVER_KEY);

    $client->setAccessToken($token); // This throws an exception

    $data = $client->verifyIdToken($token)->getAttributes();
...

$client->setAccessToken throws an exception: Could not json decode the token . Can you tell me how to verify the user login? Thank you very much.

I faced this same problem and I found out the following solution for your problem:

auth2.attachClickHandler(element, {},
        function(googleUser) {
            var link = server+"user/google-plus-login"
            console.log(link)
            console.log(googleUser)
            var Name = googleUser['wc']['Ld']
            var mail = googleUser['wc']['wc']
            var image = googleUser['wc']['zt']
            var access_token = googleUser['Ka']['access_token']
        }, function(error) {
            alert(JSON.stringify(error, undefined, 2));
        });

Hopefully this works for you as well!

For getting the client details you just pass the token to this api https://www.googleapis.com/oauth2/v3/tokeninfo?id_token= < your_token >

if (isset ( $_REQUEST['token'] ) && $_REQUEST['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>";
}

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

Hey reason is from ajax you are passing id_token and in php, you are verifying access_token in setAccessToken($token) . So to verify and set access_token you have to send access_token as well as follows:

function onSignIn(googleUser)
{
    var id_token = googleUser.getAuthResponse().id_token,
    var access_token = googleUser.getAuthResponse(true).access_token,
        profile = googleUser.getBasicProfile();

    console.log(id_token);

    $.ajax({
        url : "{link :Signgoogle:in}",
        accepts : 'json',
        type : 'post',
        data : {
            'token' : id_token,
            'access_token' : access_token
        },

and in backend check like this

public function actionIn()
{
    $token = $_POST['token'];
    $access_token = $_POST['access_token'];

    $client = new \Google_Client();
    $client->setScopes('email');
    $client->setApplicationName(self::APP_NAME);
    $client->setDeveloperKey(self::SERVER_KEY);

    $client->setAccessToken($access_token); // This will not throw exception

    $data = $client->verifyIdToken($token)->getAttributes();
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM