Search code examples
phpgoogle-gdkgoogle-mirror-api

GDK Authentication using PHP


This is my test code:

=========================================

<?php

include_once "templates/base.php";
session_start();

require_once realpath(dirname(__FILE__) . '/../autoload.php');

function get_service_client(){
 $service_client_id="540223414844-tj91ijj3u99c44bhmftb8m20tgon57hc.apps.googleusercontent.com";
 $service_email="540223414844-tj91ijj3u99c44bhmftb8m20tgon57hc@developer.gserviceaccount.com";
 $app_name="Eyenotes";
 $key_file_location='C:/Users/Yuan/Desktop/eyenotes-81e92b7df1f1.p12';
 $browser_api_key="AIzaSyAplINlgfGWGJ8pD1Bxlr8wg7i8ZNVOS1A";

$client = new Google_Client();
$client->setApplicationName("Eyenotes"); // Set your application name
$client->setClientId("540223414844-tj91ijj3u99c44bhmftb8m20tgon57hc.apps.googleusercontent.com");
$client->setDeveloperKey("AIzaSyAplINlgfGWGJ8pD1Bxlr8wg7i8ZNVOS1A");
$key = file_get_contents("C:/Users/Yuan/Desktop/eyenotes-81e92b7df1f1.p12");
$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='com.eyenotes';
$userDataArray= array();
$userData1= new Google_Service_Mirror_UserData();
$userData1->setKey('email');
$userData1->setValue("[email protected]");
$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);
echo $account;
} catch (Exception $e) {

}
}
$service_client= get_service_client();
$mirrorService = new Google_Service_Mirror($service_client);
insert_account($mirrorService, "6164da1732ea09b4", "[email protected]");
?>

==========================================

This is the exception i got:

==========================================

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/mirror/v1/accounts/6164da1732ea09b4/com.eyenotes/ccfsz.public%40gmail.com?key=AIzaSyDxnl16J8Eiwv00k_1yzw_VoRr9wD32qao: (400) Invalid Value' in D:\MySoftware\wamp\www\insert\src\Google\Http\REST.php on line 111
Google_Service_Exception: Error calling POST https://www.googleapis.com/mirror/v1/accounts/6164da1732ea09b4/com.eyenotes/ccfsz.public%40gmail.com?key=AIzaSyDxnl16J8Eiwv00k_1yzw_VoRr9wD32qao: (400) Invalid Value in D:\MySoftware\wamp\www\insert\src\Google\Http\REST.php on line 111

========================================= Parameters explanation:

account type is i provide to google.

user token is i got from authentication url which i provide to google.

browser api key is generated when create new Key in developer console.

Please tell me what's the problem in my code?

Thanks.

I catched the exception and it didn't raise exception again,but I still can't retrieving account information using mAccountManager.getAccountByType("com.eyenotes"). And in google developer console,it shows I have requested successfully. Another, I can't get my application after toggle "ON" on Glassware.Now I am trying to factory reset my glass,but after two hours,I didn't get any information that indicate i have reset success. (now my application is not publicly visible)


Solution

  • There are lot of things I want to know... Firstly the path in the error is not in your code.. So cannot be sure where the error is actually coming from.

    There may be other issues too regarding the authentication in the GDK, just to give you a heads up.... You can temporarily store the userToken query param sent with that request (don't permanently store it), and then authenticate the user by whichever backend you have running. From there you then likely request the proper scope for their Google account. Once that's done, you can create your Mirror Service as normal in PHP and then use the Accounts collection:

        // $myClient would contain the typical Google_Client() setup
    // See PHP quickstart for example
    $myMirrorService = new Google_Service_Mirror($myClient);
    
    // Set your inputs to insert() as needed
    $accounts = $myMirrorService->accounts->insert($userToken, $accountType, $accountName, $postBody);
    

    And also, you can only test and use this API after your APK lands on MyGlass (which happens during the review process). I would also recommend the Mirror API PHP Quickstart as a start point to understand how the authentication works if you have not done so already.