Example #1
0
        private Action HandlePlayerApiError(string response, Action retryAction)
        {
            return(new Action(
                       async() =>
            {
                dynamic json = JsonConvert.DeserializeObject(response);

                if ("NO_ACTIVE_DEVICE".Equals((string)json.error.reason))
                {
                    DialogResult retryResult;
                    do
                    {
                        PlayerApi playerApi = new PlayerApi()
                        {
                            AuthorizationHeader = new AuthorizationHeader(await this.RunAsync(AccessTokenTask))
                        };

                        ChooseDeviceForm chooseDeviceForm = new ChooseDeviceForm(playerApi);
                        if (chooseDeviceForm.ShowDialog(this) == DialogResult.OK)
                        {
                            await playerApi.TransferPlayback(chooseDeviceForm.SelectedDevice.Id);

                            bool isDeviceActive = false;
                            for (int i = 0; !isDeviceActive && i < 5; i++)
                            {
                                dynamic devicesJson = await playerApi.GetDevices();
                                foreach (dynamic device in devicesJson.devices)
                                {
                                    if ((string)device.id != chooseDeviceForm.SelectedDevice.Id)
                                    {
                                        continue;
                                    }

                                    isDeviceActive = device.is_active;
                                }
                            }

                            if (isDeviceActive)
                            {
                                await Task.Run(retryAction);
                                break;
                            }
                        }

                        retryResult = MessageBox.Show(
                            "Select a device to play to.",
                            "No active device",
                            MessageBoxButtons.RetryCancel,
                            MessageBoxIcon.Error);
                    }while (retryResult == DialogResult.Retry);
                }
            }));
        }
Example #2
0
        public async Task TransferPlayback_GetDevice1IdPlayTrue_NoException()
        {
            // arrange
            var        http        = new HttpClient();
            IPlayerApi player      = new PlayerApi(http);
            string     accessToken = await TestsHelper.GetUserAccessToken();

            var devices = await player.GetDevices(accessToken : accessToken);

            // act
            if (devices.Any())
            {
                await player.TransferPlayback(devices.Last().Id, play : true, accessToken : accessToken);
            }
        }
Example #3
0
        //[TestMethod]
        public async Task Usage2()
        {
            // Get a list of a User's devices
            // This requires User authentication and authorization.
            // A `UserAccountsService` is provided to help with this.

            // HttpClient and UserAccountsService can be reused.
            // Tokens can be cached by your code
            var http     = new HttpClient();
            var accounts = new UserAccountsService(http, TestsHelper.GetLocalConfig());

            // See https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
            //  for an explanation of the Authorization code flow

            // Generate a random state value to use in the Auth request
            string state = Guid.NewGuid().ToString("N");
            // Accounts service will derive the Auth URL for you
            string url = accounts.AuthorizeUrl(state, new[] { "user-read-playback-state" });

            /*
             *  Redirect the user to `url` and when they have auth'ed Spotify will redirect to your reply URL
             *  The response will include two query parameters: `state` and `code`.
             *  For a full working example see `SpotifyApi.NetCore.Samples`.
             */
            var query = new Dictionary <string, string>();

            // Check that the request has not been tampered with by checking the `state` value matches
            if (state != query["state"])
            {
                throw new ArgumentException();
            }

            // Use the User accounts service to swap `code` for a Refresh token
            BearerAccessRefreshToken token = await accounts.RequestAccessRefreshToken(query["code"]);

            // Use the Bearer (Access) Token to call the Player API
            var player = new PlayerApi(http, accounts);

            Device[] devices = await player.GetDevices(accessToken : token.AccessToken);

            foreach (Device device in devices)
            {
                Trace.WriteLine($"Device {device.Name} Status = {device.Type} Active = {device.IsActive}");
            }
        }