rapidoodle
rapidoodle

Reputation: 350

Google Glass User Authentication in PHP

Good day. I'm trying to create a authentication page in my glassware as google requirement using PHP and I'm stuck for days. I followed instruction here but can't make it work.

This is my code:

<?php
    require_once('dbconnect.php');
    include_once 'google-api-php-client/src/Google_Client.php';
    include_once 'google-api-php-client/src/contrib/Google_MirrorService.php';
    include_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';

    function get_service_client()
    {
        global $service_client_id, $service_email, $app_name, $key_file_location, $browser_api_key;
        $client = new Google_Client();

        $client->setApplicationName("Google Glass"); // Set your application name
        $client->setClientId("CLIENT_ID");
        $client->setDeveloperKey("DEVELOPER_KEY");

        $key = file_get_contents("KEY_FILE_PATH");
        $cred = new Google_Auth_AssertionCredentials(
                    $service_email,
                    array('https://www.googleapis.com/auth/glass.thirdpartyauth'),
                    $key
                );
        $client->setAssertionCredentials($cred);
        return $client;
    }

    function insert_account($service,$userToken, $email)
    {
        $accountType ='info.google.glass';
        $userDataArray = array();
        $userData1 = new Google_Service_Mirror_UserData();

        $userData1->setKey('email');
        $userData1->setValue($email);

        $userDataArray[] = $userData1;
        $authTokenArray = array();
        $authToken1 = new Google_Service_Mirror_AuthToken();

        $authToken1->setAuthToken('randomtoken');
        $authToken1->setType('randomType');
        $authTokenArray[]=$authToken1;
        $postBody = new Google_Service_Mirror_Account();
        $postBody->setUserData($userDataArray);
        $postBody->setAuthTokens($authTokenArray);

        try {
            $account = $service->accounts->insert($userToken, 
                $accountType, 
                $email, 
                $postBody
            );
        } catch (Exception $e) {
            echo "fail";
        }
    }

    if (!$conn) {
        die("Connection failed: " . mysql_error());
    }
    else 
    {
        if(isset($_POST['submit']))
        {
            $tempuname =isset($_POST['uname'])?mysql_real_escape_string($_POST['uname']) : "";
            $temppass   = isset($_POST['pass'])  ? mysql_real_escape_string($_POST['pass'])  : "";
            $pass       = sha1($temppass);

            $uarray     = explode("/", $tempuname);

            //var_dump($uarray);
            $dbname     = $uarray[0];
            $uname      = mysql_real_escape_string($uarray[1]);
            $db         = mysql_select_db("genix_".$dbname);


                if($db && isset($uname)) 
                {
                    $sql    = mysql_query("SELECT * FROM users WHERE username = '".$uname."' AND password = '".$pass."'");
                    if( $sql )
                    {
                        while($row = mysql_fetch_array($sql)) 
                        {
                                $userToken      = $_SESSION['userToken'];
                                $service_client = get_service_client();
                                $mirrorService  = new Google_Service_Mirror($service_client);
                                insert_account($mirrorService, $userToken, $email);
                                $_SESSION['userToken']=null;
                                echo $row['facility'];
                        }
                    }
                else {
?>
<script>
    alert("Invalid Login.");
</script>
<?
    }
} else {
?>
<script>
    alert("Invalid Login.");
</script>
<?      
        }
    }
    // }
}

I don't received any responses nor error after submit. Can please you tell me where i went wrong? Thank you so much.

Upvotes: 0

Views: 67

Answers (1)

Koh
Koh

Reputation: 1570

I see that you are trying to insert an account. If you haven't done so, you will need to go through the review process to use this function. As mentioned in the docs, you will need to have your apk uploaded to MyGlass to test/use the API.

Upvotes: 1

Related Questions