2015-04-07 38 views
0

美好的一天。我试图在我的玻璃器皿中创建一个认证页面,因为使用PHP的谷歌需求,我坚持了几天。我遵循指令here但无法使其工作。PHP中的Google Glass用户身份验证

这是我的代码:

<?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> 
<?  
     } 
    } 
    // } 
} 

我没有收到任何回应,也没有错误提交后。可以请你告诉我哪里出错了?非常感谢。

+0

你采取了哪些步骤来调试?我建议你检查一下哪些行/方法调用你的代码开始无法按预期工作。 – Koh

+0

调试后,我发现我没有收到任何'userToken',并导致问题。任何帮助? – rapidoodle

回答

1

我看到您正试图插入一个帐户。如果你还没有这样做,你将需要通过审查过程来使用这个功能。正如文档中提到的,您需要将您的apk上传到MyGlass以测试/使用API​​。

+0

你说得对。谢谢。 – rapidoodle