PHP_SMART_HOME_V3/app/plugins/Spotify.php

67 lines
2.0 KiB
PHP
Raw Normal View History

2020-10-03 16:21:31 +00:00
<?php
2020-10-22 14:42:49 +00:00
class Spotify extends VirtualDeviceManager
{
2020-10-03 16:21:31 +00:00
private $token = "";
2020-10-03 19:44:09 +00:00
private $client_id = '76840e2199e34dcd903d19877bd726dd'; // Your client id
private $redirect_uri = 'https://dev.steelants.cz/vasek/home-update/plugins/spotify/callback'; // Your redirect uri
2020-10-22 14:42:49 +00:00
public function oAuth()
{
2020-10-03 19:44:09 +00:00
$client_secret = 'CLIENT_SECRET'; // Your secret
$scopes = 'user-read-private user-read-email';
2020-10-22 14:42:49 +00:00
header('Location: https://accounts.spotify.com/authorize?client_id=' . $this->client_id . '&response_type=token&redirect_uri=' . urlencode($this->redirect_uri) . '&scope=user-read-playback-state');
2020-10-03 19:44:09 +00:00
}
2020-10-03 16:21:31 +00:00
2020-10-22 14:42:49 +00:00
private function setToken($token)
{
2020-10-03 16:21:31 +00:00
$this->token = $token;
}
2020-10-22 14:42:49 +00:00
public function callback()
{
2020-10-05 19:12:06 +00:00
var_dump($_REQUEST);
2020-10-03 19:44:09 +00:00
(new SettingsManager)->create('spotify_token', $token);
}
2020-10-22 14:42:49 +00:00
public function autorize()
{
2020-10-03 19:44:09 +00:00
2020-10-03 16:21:31 +00:00
$client_secret = '0f94ed2c0bd64bf791ea13b7e6310ba3';
$ch = curl_init();
2020-10-22 14:42:49 +00:00
curl_setopt($ch, CURLOPT_URL, 'https://accounts.spotify.com/api/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&scope=user-read-playback-state');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode($this->client_id . ':' . $client_secret)));
2020-10-03 16:21:31 +00:00
2020-10-22 14:42:49 +00:00
$result = curl_exec($ch);
2020-10-03 16:21:31 +00:00
$this->setToken(json_decode($result, true)['access_token']);
echo $result;
}
2020-10-22 14:42:49 +00:00
private function getPlayerData()
{
2020-10-03 16:21:31 +00:00
$ch = curl_init();
2020-10-22 14:42:49 +00:00
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/me/player');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
2020-10-03 19:44:09 +00:00
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . (new SettingsManager)->getByName('spotify_token')['value']));
2020-10-03 16:21:31 +00:00
2020-10-22 14:42:49 +00:00
$result = curl_exec($ch);
2020-10-03 16:21:31 +00:00
echo $result;
}
2020-10-22 14:42:49 +00:00
// function make()
// {
// try {
// //$this->autorize();
// //$this->getPlayerData();
// return 'sucessful';
// } catch (Exception $e) {
// return 'exception: ' . $e->getMessage();
// }
// }
2020-10-03 16:21:31 +00:00
}