Reputation: 156
I am currently creating a smarthome automation. I am using Laravel PHP in my cloud. I am done with Oauth2 Authentication and sending commands from Google Home App to my devices (I used MQTT to communicate with my devices).
I have already implemented successfully SYNC
, QUERY
and EXECUTE
. and now i want to synchronize the rooms and structures for which i have to use the Homegraph API. Is there somebody who can guide me in intergrating the Homegraph APi to my Smarthome because i am facing problem here in JWT authentication? My app is based on laravel so i am not able to follow the sample codes. I tried like this:
$jsonFile = json_decode(file_get_contents('./key.json'), true);
$client = new \GuzzleHttp\Client();
$payload = [
"iss" => $jsonFile['client_email'],
"scope" => "https://www.googleapis.com/auth/homegraph",
"aud" => "https://accounts.google.com/o/oauth2/token",
"iat" => date("h:i:sa"),
"exp" => date("h:i:sa", strtotime('+1 hour'))
];
$jwt = JWT::encode($payload, $jsonFile["private_key"], 'RS256');
$header = array("Authorization" => "Bearer " . $jwt, "Content-Type" => "application/json");
try {$response = $client->get( "https://accounts.google.com/o/oauth2/token", [
"headers" => $header]);
} catch (\Exception $e) {
dd($e);
}
I am getting an error of
Client error: `GET https://accounts.google.com/o/oauth2/token` resulted in a `404 Not Found` response:
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, w (truncated...)
i am following this http post link
Upvotes: 1
Views: 803
Reputation: 10504
Using Google APIs Client Library for PHP, should allow you to integrate directly w/ the Homegraph API from a PHP based Smart Home fulfilment without having to handcraft HTTP request and authorization.
It can leverages Google Application Default credentials to authorize the application using a service account key:
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/homegraph");
It includes generated bindings for the Homegraph API:
$homegraphService = new Google_Service_HomeGraphService($client);
$homegraphService->devices->reportStateAndNotification(...);
And generated types for all Homegraph API request and response payload, for example implementing Report State would look like this:
$request = new Google_Service_HomeGraphService_ReportStateAndNotificationRequest();
$request->setAgentUserId("placeholder-user-id");
$request->setRequestId("placeholder-request-id");
$payload = new Google_Service_HomeGraphService_StateAndNotificationPayload();
$devices = new Google_Service_HomeGraphService_ReportStateAndNotificationDevice();
$devices->setStates(array(
"placeholder-device-id" => array(
"placeholder-state-key" => ...
)
));
$payload->setDevices($devices);
$request->setPayload($payload);
$homegraphService->devices->reportStateAndNotification($request));
Upvotes: 2