It is possible to authenticate a Google user ( the one logged in on an android device ) to my server using the auth_token obtained form Google?

前端 未结 1 641
南笙
南笙 2021-01-03 06:49

I have an Android app which let the users to add content to my server. Each user should have an account on this server.

The app communicate with the server through

相关标签:
1条回答
  • 2021-01-03 07:28

    There should be a way of sending the auth_token to google and retrieving information like the google user_ID since you can retrieve those infos :

    {
      "audience":"8819981768.apps.googleusercontent.com",
      "user_id":"123456789",
      "scope":"https://gdata.youtube.com",
      "expires_in":436
    }
    

    by making a POST from your android app on : https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=1/fFBGRNJru1FQd44AzqT3Zg with the access token ... It's even indicated in https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Authentication The problem is that when you want to do it from your php server you'll have to send it in SSL because google requires it ... Here is my code :

    <?php
    
    if( isset($_POST['authToken'])){        
    
        header('Content-Type: text/plain');
    
        $name = 'www.googleapis.com';//nom du site
    
        $data = "access_token='".$_POST['authToken']."' ";
    
        $envoi  = "POST /oauth2/v1/tokeninfo HTTP/1.1\r\n";
        $envoi .= "Host: ".$name."\r\n";
        $envoi .= "Connection: Close\r\n";
        $envoi .= "Content-type: application/x-www-form-urlencoded\r\n";
        $envoi .= "Content-Length: ".strlen($data)."\r\n\r\n";
        $envoi .= $data."\r\n";      
    
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if($socket < 0){
                die('FATAL ERROR: socket_create() : " '.socket_strerror($socket).' "');
        }
    
        if (socket_connect($socket,gethostbyname($name),80) < 0){
                die('FATAL ERROR: socket_connect()');
        }
    
    
        if(($int = socket_write($socket, $envoi, strlen($envoi))) === false){
                die('FATAL ERROR: socket_write() failed, '.$int.' characters written');
        }
    
        $reception = '';
        while($buff = socket_read($socket, 2000)){
           $reception.=$buff;
        }
        echo $reception;
    
        socket_close($socket);  
    }
    ?>
    

    it just needs to be adapted to SSL and it should work to retrieve the google user_id ! After what you can compare it with the one in your database and the logg is done !

    By the way, could you share your code with us (using your method with auth_token for contacts) ? or send it to fakeclark@live.com THANKS ;-)

    0 讨论(0)
提交回复
热议问题