{"action":"created","issue":{"url":"https://api.github.com/repos/jwilsson/spotify-web-api-php/issues/100","repository_url":"https://api.github.com/repos/jwilsson/spotify-web-api-php","labels_url":"https://api.github.com/repos/jwilsson/spotify-web-api-php/issues/100/labels{/name}","comments_url":"https://api.github.com/repos/jwilsson/spotify-web-api-php/issues/100/comments","events_url":"https://api.github.com/repos/jwilsson/spotify-web-api-php/issues/100/events","html_url":"https://github.com/jwilsson/spotify-web-api-php/issues/100","id":263423226,"number":100,"title":"Authentication example could be better","user":{"login":"andersborgabiro","id":1548064,"avatar_url":"https://avatars3.githubusercontent.com/u/1548064?v=4","gravatar_id":"","url":"https://api.github.com/users/andersborgabiro","html_url":"https://github.com/andersborgabiro","followers_url":"https://api.github.com/users/andersborgabiro/followers","following_url":"https://api.github.com/users/andersborgabiro/following{/other_user}","gists_url":"https://api.github.com/users/andersborgabiro/gists{/gist_id}","starred_url":"https://api.github.com/users/andersborgabiro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andersborgabiro/subscriptions","organizations_url":"https://api.github.com/users/andersborgabiro/orgs","repos_url":"https://api.github.com/users/andersborgabiro/repos","events_url":"https://api.github.com/users/andersborgabiro/events{/privacy}","received_events_url":"https://api.github.com/users/andersborgabiro/received_events","type":"User","site_admin":false},"labels":[{"id":714956911,"url":"https://api.github.com/repos/jwilsson/spotify-web-api-php/labels/docs","name":"docs","color":"5319e7","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2017-10-06T12:02:34Z","updated_at":"2017-10-17T23:29:59Z","closed_at":null,"author_association":"NONE","body":"This library provides a great and broad abstraction of the Spotify Web API. Thank you for that.\r\n\r\nThe authentication example is not so good though, and doesn't work the way it's written.\r\n\r\nThis is how I did it. Note that everything is done in a single script, including all app state.\r\n\r\nThe try/catch should probably cover more than it does.\r\n\r\n```\r\ndefine('CLIENT_ID', 'xxxxxxxxxxxxxxxxxxxxxx');\r\ndefine('CLIENT_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxx');\r\ndefine('REDIRECT_URI', \"http\" . (!empty($_SERVER['HTTPS']) ? \"s\" : \"\") . \"://\" . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']);\r\ndefine('SESSION', 'spotify_access_token');\r\n\r\n...\r\n\r\n$session = new SpotifyWebAPI\\Session(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);\r\n\r\nif (isset($_GET['code'])):\r\n    $session->requestAccessToken($_GET['code']);\r\n    $accessToken = $session->getAccessToken();\r\n    $_SESSION[SESSION] = $accessToken;\r\n    header('Location: index.php');\r\n    die();\r\nelseif (!isset($_SESSION[SESSION])):\r\n    $options = ['scope' =>\r\n        [\r\n            'user-read-private',\r\n            'user-read-email',\r\n            'user-read-birthdate',\r\n            'user-top-read',\r\n            'playlist-read-private',\r\n            'playlist-modify-private',\r\n            'playlist-read-collaborative',\r\n            'playlist-modify-public',\r\n            'ugc-image-upload'\r\n        ]\r\n    ];\r\n    header('Location: ' . $session->getAuthorizeUrl($options));\r\n    die();\r\nelse:\r\n    $accessToken = $_SESSION[SESSION];\r\nendif;\r\n\r\ntry {\r\n    $api = new SpotifyWebAPI\\SpotifyWebAPI();\r\n    $api->setAccessToken($accessToken);\r\n    $api->setReturnType(SpotifyWebAPI\\SpotifyWebAPI::RETURN_ASSOC);\r\n\r\n    $me = $api->me();\r\n    $userName = $me['display_name'];\r\n    $userId = $me['id'];\r\n    $userPicture = $me['images'][0]['url'];\r\n}\r\ncatch (Exception $ex) {\r\n    unset($_SESSION[SESSION]);\r\n    header('Location: index.php');\r\n    die();\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/jwilsson/spotify-web-api-php/issues/comments/337408314","html_url":"https://github.com/jwilsson/spotify-web-api-php/issues/100#issuecomment-337408314","issue_url":"https://api.github.com/repos/jwilsson/spotify-web-api-php/issues/100","id":337408314,"user":{"login":"andersborgabiro","id":1548064,"avatar_url":"https://avatars3.githubusercontent.com/u/1548064?v=4","gravatar_id":"","url":"https://api.github.com/users/andersborgabiro","html_url":"https://github.com/andersborgabiro","followers_url":"https://api.github.com/users/andersborgabiro/followers","following_url":"https://api.github.com/users/andersborgabiro/following{/other_user}","gists_url":"https://api.github.com/users/andersborgabiro/gists{/gist_id}","starred_url":"https://api.github.com/users/andersborgabiro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andersborgabiro/subscriptions","organizations_url":"https://api.github.com/users/andersborgabiro/orgs","repos_url":"https://api.github.com/users/andersborgabiro/repos","events_url":"https://api.github.com/users/andersborgabiro/events{/privacy}","received_events_url":"https://api.github.com/users/andersborgabiro/received_events","type":"User","site_admin":false},"created_at":"2017-10-17T23:29:59Z","updated_at":"2017-10-17T23:29:59Z","author_association":"NONE","body":"Then try the authorization code flow (if you intend to affect user data). It's not much harder, except there are more states, and if you want to keep the token for long you need to use the refresh token. In my tests the access token seemed to always last an hour, and my app is fine with that, so I currently don't use the refresh token. The app does a \"soft fall\" when/if the token expires."}}
{"actor":{"display_login":"andersborgabiro"}}