public static function connect($accessToken) { static::assertValidAccessToken($accessToken); $api = new SpotifyWebAPI(); $api->setAccessToken($accessToken); return new static($api); }
/** * Search for a track and build the response objects * * @param string $string * * @return Track */ public function findTrack(string $string) { $result = $this->spotifyWebApi->search($string, 'track'); if (count($result->tracks->items) === 0) { throw NoTracksFoundException::emptyResult(); } $firstItem = $result->tracks->items[0]; $images = $this->valueObjectBuilder->buildImages($firstItem->album->images); $album = $this->valueObjectBuilder->buildAlbumSimplified($firstItem->album, $images); $artists = $this->valueObjectBuilder->buildArtistsSimplified($firstItem->artists); $track = $this->valueObjectBuilder->buildTrack($firstItem, $album, $artists); return $track; }
public function register(Application $app) { $app['spotify.api'] = $app->share(function () use($app) { $api = new ThirdPartApi(); if (!($auth_token = $app['predis']->get('spotify:auth'))) { $session = new \SpotifyWebAPI\Session(Credentials::CLIENT_ID, Credentials::CLIENT_SECRET, ''); $session->requestCredentialsToken(array()); $auth_token = $session->getAccessToken(); $app['predis']->set('spotify:auth', $auth_token); $app['predis']->expire('spotify:auth', $session->getExpires() - 60); } $api->setAccessToken($auth_token); return new RuntimeCacheDecorator(new SpotifyApi($api)); }); }
/** * {@inheritdoc} */ protected function authHeaders() { $token = $this->tokenStorage->getToken(); // Save the access token if user is logged in if ($token instanceof TokenInterface && $token->getUser() instanceof SpotifyUser) { $this->setAccessToken($this->tokenStorage->getToken()->getUser()->getAccessToken()); } return parent::authHeaders(); }
/** * (non-PHPdoc) * @see \API\src\Endpoints\Endpoints::get() */ public function get() { $session = new Session(Config::getConfig('SpotifyAppId'), Config::getConfig('SpotifySecret')); $callback = 'http://api.soundeavor.com/User/Auth/Login/Spotify/index.php'; $session->setRedirectUri($callback); $url = $session->getAuthorizeUrl(); if (!isset($_GET['code'])) { header('Location: ' . $url); } $code = $_GET['code']; $api = new SpotifyWebAPI(); $session->requestToken($code); $api->setAccessToken($session->getAccessToken()); $me = $api->me(); $body['spotifyName_text'] = $me->display_name; $body['spotifyAccessToken_text'] = $session->getAccessToken(); $body['spotifyUserId_text'] = $me->id; $this->request->response->body = $body; $this->request->response->code = r_success; }
public function testGetReturnAssoc() { $request = $this->getMock('SpotifyWebAPI\\Request'); $request->expects($this->once())->method('getReturnAssoc')->willReturn(true); $api = new SpotifyWebAPI\SpotifyWebAPI($request); $this->assertTrue($api->getReturnAssoc(true)); }
<?php use SpotifyWebAPI\SpotifyWebAPI; use SpotifyWebApiExtensions\GuzzleClientFactory; use SpotifyWebApiExtensions\GuzzleRequestAdapter; use Doctrine\Common\Cache\FilesystemCache; require_once './vendor/autoload.php'; $guzzleAdapter = new GuzzleRequestAdapter(GuzzleClientFactory::create(new FilesystemCache(__DIR__ . '/cache'))); $api = new SpotifyWebAPI($guzzleAdapter); print_r($api->search('Nothing else matters', ['track'], ['market' => 'DE']));
/** * @param Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function showPlaylistList(Request $request) { $session = new SpotifyWebAPI\Session(Config::get('spotify.client_id'), Config::get('spotify.client_secret'), Config::get('spotify.redirect_url.showPlaylistList')); $api = new SpotifyWebAPI\SpotifyWebAPI(); try { if (isset($_GET['code'])) { $session->requestAccessToken($_GET['code']); $api->setAccessToken($session->getAccessToken()); } else { header('Location: ' . $session->getAuthorizeUrl(array('scope' => array('playlist-modify-private', 'playlist-modify-public', 'playlist-read-private')))); die; } } catch (Exception $e) { Log::error($e->getMessage()); return redirect('/spotify/step_one'); } if (!$request->session()->get('spotify.user_id') && $request->session()->get('spotify.playlist_id')) { return redirect('/spotify/step_one'); } $playlistTracks = $api->getUserPlaylistTracks($request->session()->get('spotify.user_id'), $request->session()->get('spotify.playlist_id')); $spotifyPlaylist = array(); $data = array(); foreach ($playlistTracks->items as $track) { $track = $track->track; $spotifyPlaylist[$track->id] = $track->name . ' ' . $track->artists[0]->name; $data[] = array('id' => $track->id, 'name' => $track->name, 'artists' => $track->artists[0]->name); } $request->session()->put('spotify.songs', $spotifyPlaylist); return View::make('spotify.step_four')->with('data', $data); }
/** * @expectedException \AppBundle\Exception\NoTracksFoundException */ public function testFindTrackEmptyResultThrowsError() { $result = (object) ['tracks' => (object) ['items' => []]]; $this->apiStub->method('search')->willReturn($result); $this->trackFinder->findTrack('track that doesnt exist'); }