コード例 #1
0
        private static void OldWay()
        {
            //Create the auth object
            auth = new AutorizationCodeAuth()
            {
                //Your client Id
                ClientId = "4ba8934628a54571b57ed84de51d1825",
                //Set this to localhost if you want to use the built-in HTTP Server
                RedirectUri = "http://localhost:8000",
                //How many permissions we need?
                Scope      = Scope.UserReadPrivate | Scope.PlaylistModifyPublic | Scope.PlaylistModifyPrivate,
                ShowDialog = true
            };
            //This will be called, if the user cancled/accept the auth-request
            auth.OnResponseReceivedEvent += auth_OnResponseReceivedEvent;
            //a local HTTP Server will be started (Needed for the response)
            auth.StartHttpServer(8000);
            //This will open the spotify auth-page. The user can decline/accept the request

            try
            {
                auth.DoAuth();
                Thread.Sleep(60000);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            auth.StopHttpServer();
            Console.WriteLine("Too long, didnt respond, exiting now...");
        }
コード例 #2
0
ファイル: AuthHelper.cs プロジェクト: eltariel/MicroSpot
        public bool Authorise()
        {
            if (config.Comms.AuthToken == null)
            {
                auth.StartHttpServer(8888);
                auth.DoAuth();
                return(false);
            }

            RefreshToken();
            return(true);
        }
コード例 #3
0
 private void OnLoginClick(object sender, RoutedEventArgs e)
 {
     //Create the auth object
     auth = new AutorizationCodeAuth
     {
         ClientId    = Configuration.Comms.ClientId,
         RedirectUri = "http://localhost:8888",
         Scope       = (Scope)0x1FFFF,
     };
     auth.OnResponseReceivedEvent += OnAuthResponseReceived;
     auth.StartHttpServer(8888);
     auth.DoAuth();
 }
コード例 #4
0
ファイル: Parent.cs プロジェクト: azerial/SpotifyPlugin
        public void Authenticate()
        {
            AutorizationCodeAuth authentication = new AutorizationCodeAuth
            {
                RedirectUri = new UriBuilder("http://127.0.0.1")
                {
                    Port = 7476
                }.Uri.OriginalString.TrimEnd('/'),
                ClientId = _clientId,
                Scope    = _scope,
                State    = "XSS"
            };

            // Try refreshing
            var token = authentication.RefreshToken(Properties.Settings.Default.RefreshToken, _clientSecret);

            if (token.Error == null)
            {
                WebAPI = ApiFromToken(token);
                return;
            }

            AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false);

            WebAPI = null;
            authentication.OnResponseReceivedEvent += (response) =>
            {
                WebAPI = HandleSpotifyResponse(response, authentication);
                authenticationWaitFlag.Set();
            };

            try
            {
                authentication.StartHttpServer(7476);

                authentication.DoAuth();

                authenticationWaitFlag.WaitOne(TimeSpan.FromSeconds(_timeout));
                if (WebAPI == null)
                {
                    throw new TimeoutException($"No valid response received for the last {_timeout} seconds");
                }
            }
            finally
            {
                authentication.StopHttpServer();
            }
        }
コード例 #5
0
        public void RequestAuth(AutorizationCodeAuth.OnResponseReceived codeReceiveCallback)
        {
            if (AuthRequesting)
            {
                return;
            }

            AuthRequesting = true;

            AutorizationCodeAuth auth = new AutorizationCodeAuth();

            auth.ClientId    = ClientId;
            auth.RedirectUri = RedirUri;
            auth.Scope       = Scope;

            auth.OnResponseReceivedEvent += response =>
            {
                AuthRequesting = false;
                auth.StopHttpServer();
                codeReceiveCallback.Invoke(response);
            };

            auth.StartHttpServer(RedirPort);

            auth.DoAuth();

            int ms = 0;

            while (ms < 30000)
            {
                if (!AuthRequesting)
                {
                    break;
                }

                Thread.Sleep(100);
                ms += 100;
            }

            if (AuthRequesting)
            {
                auth.StopHttpServer();
                AuthRequesting = false;
            }
        }
コード例 #6
0
        private static void Main()
        {
            using (var sr = new StreamReader("data/clientData.txt"))
            {
                _auth = new AutorizationCodeAuth()
                {
                    ClientId    = sr.ReadLine(),
                    RedirectUri = "http://localhost",
                    Scope       = Scope.UserReadPrivate,
                };
                _auth.StartHttpServer();
                _clientSecret = sr.ReadLine();
                _auth.OnResponseReceivedEvent += _callback;
                _auth.DoAuth();
            }

            Console.ReadLine();
        }
コード例 #7
0
        private void Autorization()
        {
            auth.StartHttpServer();
            auth.DoAuth();
            Thread.Sleep(1500);
            Token a = auth.ExchangeAuthCode(SpotifyLogin.GrantCode, SpotifyLogin.GetClient_Secret);

            headers.Add("Authorization", "Bearer " + a.AccessToken);

            if (a != null)
            {
                SpotifyLogin.AccessToken  = a.AccessToken;
                SpotifyLogin.RefreshToken = a.RefreshToken;
                SpotifyWindow main = new SpotifyWindow();
                main.Show();
                Close();
            }
        }
コード例 #8
0
 public void Run()
 {
     _auth = new AutorizationCodeAuth()
     {
         //Your client Id
         ClientId = "29876bdcb6384fc287e37ae01873058c",
         //Set this to localhost if you want to use the built-in HTTP Server
         RedirectUri = "http://localhost:7777/",
         //How many permissions we need?
         Scope = Scope.UserReadPrivate,
     };
     //This will be called, if the user cancled/accept the auth-request
     _auth.OnResponseReceivedEvent += Auth_OnResponseReceivedEvent;
     //a local HTTP Server will be started (Needed for the response)
     _auth.StartHttpServer(7777);
     //This will open the spotify auth-page. The user can decline/accept the request
     _auth.DoAuth();
 }
コード例 #9
0
        public Task <SpotifyWebAPI> GetWebApi()
        {
            var authentication = new AutorizationCodeAuth
            {
                RedirectUri = $"{_redirectUrl}:{_listeningPort}",
                ClientId    = _clientId,
                Scope       = _scope,
                State       = _xss
            };

            AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false);
            SpotifyWebAPI  spotifyWebApi          = null;

            authentication.OnResponseReceivedEvent += (response) =>
            {
                var state = response.State;
                var token = authentication.ExchangeAuthCode(response.Code, _clientSecret);
                spotifyWebApi = HandleSpotifyResponse(state, token);



                authenticationWaitFlag.Set();
            };

            try
            {
                authentication.StartHttpServer(_listeningPort);

                authentication.DoAuth();

                authenticationWaitFlag.WaitOne(_timeout);
                if (spotifyWebApi == null)
                {
                    throw new TimeoutException($"No valid response received for the last {_timeout.TotalSeconds} seconds");
                }
            }
            finally
            {
                authentication.StopHttpServer();
            }

            return(Task.FromResult(spotifyWebApi));
        }
コード例 #10
0
        public static SpotifyWebAPI GetInstanceAuth()
        {
            auth = new AutorizationCodeAuth()
            {
                //Your client Id
                spotifyClientID = MYSPOTIFY_CLIENTID,
                //Set this to localhost if you want to use the built-in HTTP Server
                RedirectUri = "http://localhost:8888",
                //How many permissions we need?
                Scope = Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.PlaylistModifyPrivate | Scope.PlaylistModifyPublic | Scope.UserLibraryRead |
                        Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead | Scope.PlaylistReadCollaborative |
                        Scope.UserReadRecentlyPlayed | Scope.UserReadPlaybackState | Scope.UserModifyPlaybackState,
            };
            //This will be called, if the user cancled/accept the auth-request
            auth.OnResponseReceivedEvent += auth_OnResponseReceivedEvent;
            //a local HTTP Server will be started (Needed for the response)
            auth.StartHttpServer();
            //This will open the spotify auth-page. The user can decline/accept the request
            auth.DoAuth();

            Thread.Sleep(10000);

            return(SPOTIFY_INST);
        }
コード例 #11
0
        /// <summary>
        ///
        /// </summary>
        private void DoAuth()
        {
            //Create the auth object
            auth = new AutorizationCodeAuth()
            {
                //Your client Id
                ClientId = "ce833ef1272046489c5cb86af277d9c0",
                //Set this to localhost if you want to use the built-in HTTP Server
                RedirectUri = "http://localhost",
                //RedirectUri = "http://treaiapi20170628103254.azurewebsites.net",
                //How many permissions we need?
                Scope = Scope.All
            };
            //This will be called, if the user cancled/accept the auth-request
            auth.OnResponseReceivedEvent += auth_OnResponseReceivedEvent;
            //a local HTTP Server will be started (Needed for the response)
            auth.StartHttpServer();
            //This will open the spotify auth-page. The user can decline/accept the request
            auth.DoAuth();

            Thread.Sleep(5000);
            //auth.StopHttpServer();
            Console.WriteLine("Too long, didnt respond, exiting now...");
        }
コード例 #12
0
        private static void Authorize(CommandLineApplication command)
        {
            var clientIdOption = command.Option("-c|--clientId <clientId>", "The application client id", CommandOptionType.SingleValue);

            var clientSecretOption = command.Option("-k|--clientSecret <clientSecret>", "The application client secret", CommandOptionType.SingleValue);

            var scopeOption = command.Option("-s|--scope <scope>", "The application client secret", CommandOptionType.MultipleValue);

            var outputOption = command.Option("-o|--output <path>", "Path to output token as json", CommandOptionType.SingleValue);

            var browserPathOption = command.Option("-b|--browser <path>", "Path to the browser to start", CommandOptionType.SingleValue);

            command.OnExecute(
                () =>
            {
                TaskCompletionSource <string> tcs = new TaskCompletionSource <string>();

                Scope scope = scopeOption.Values
                              .Select(v => Enum.Parse <Scope>(v))
                              .Aggregate(new Scope(), (a, s) => a | s);

                var auth = new AutorizationCodeAuth()
                {
                    ClientId      = clientIdOption.Value(),
                    RedirectUri   = "http://localhost",
                    Scope         = scope,
                    ShowDialog    = true,
                    OpenUriAction = uri => Process.Start(new ProcessStartInfo {
                        FileName = browserPathOption.Value(), Arguments = uri
                    })
                };

                auth.OnResponseReceivedEvent += response =>
                {
                    Token token = auth.ExchangeAuthCode(response.Code, clientSecretOption.Value());
                    string json = JsonConvert.SerializeObject(token);
                    tcs.SetResult(json);
                };

                try
                {
                    try
                    {
                        auth.StartHttpServer();
                        auth.DoAuth();

                        Task.WaitAll(new[] { tcs.Task }, TimeSpan.FromSeconds(30));

                        using (var stream = File.OpenWrite(outputOption.Value()))
                        {
                            using (var writer = new StreamWriter(stream))
                            {
                                writer.Write(tcs.Task.Result);
                            }
                        }

                        Console.WriteLine($"Successfully wrote token to '{outputOption.Value()}'");

                        return(0);
                    }
                    finally
                    {
                        auth.StopHttpServer();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Failed to retrieve token: '{e.Message}'");

                    return(-1);
                }
            }
                );
        }