private static SpotifyWebAPI GetWebClient(SpotifyUser oSpotifyUser)
        {
            AutorizationCodeAuth oAuth = new AutorizationCodeAuth()
            {
                ClientId = _ClientPublic,
                RedirectUri= _RedirectUrl,
                Scope = Scope.UserReadPrivate | Scope.UserReadPrivate | Scope.PlaylistReadPrivate | Scope.UserLibraryRead |  Scope.UserReadPrivate | Scope.UserFollowRead
            };

            //oAuth.StartHttpServer();//Not sure if this is needed or not but what the hell why not!

            if (oSpotifyUser.AccessToken.AccessExpired)//The user has authorized us and was tokenized but the temp access token has expired
            {
                Token oToken = oAuth.RefreshToken(oSpotifyUser.RefreshToken.Code, _ClientPrivate);
                if (oToken.Error == null)
                {
                    oSpotifyUser.UpdateUserWithToken(oToken);
                }
            }

            //oAuth.StopHttpServer();
            SpotifyWebAPI oWebClient = new SpotifyWebAPI()
            {
                AccessToken = oSpotifyUser.AccessToken.Code,
                TokenType = oSpotifyUser.AccessToken.TokenType,
                UseAuth = true
            };

            return oWebClient;
        }
Exemplo n.º 2
0
        public RedirectResult Authorized(string code, string error, string state)
        {
            if (error != null)
            {
                throw new Exception(error);
            }
            AutorizationCodeAuth oAutorizationCodeAuth = new AutorizationCodeAuth()
            {
                //Your client Id
                ClientId = this._oSCCManager.SCC_PUBLIC_ID,
                //Set this to localhost if you want to use the built-in HTTP Server
                RedirectUri = this._oSCCManager.RedirectUrl,
                //How many permissions we need?
                Scope = Scope.UserReadPrivate | Scope.UserReadPrivate | Scope.PlaylistReadPrivate | Scope.UserLibraryRead | Scope.UserReadPrivate | Scope.UserFollowRead
            };

            Token oToken;
            oToken = oAutorizationCodeAuth.ExchangeAuthCode(code, this._oSCCManager.SCC_PRIVATE_ID);
            //oToken = oAutorizationCodeAuth.RefreshToken(response.Code, SCC_PRIVATE_ID);

            SpotifyWebAPI oSpotifyWebApi = new SpotifyWebAPI()
            {
                AccessToken = oToken.AccessToken,
                TokenType = oToken.TokenType,
                UseAuth = true
            };

            PrivateProfile oPrivateProfile = oSpotifyWebApi.GetPrivateProfile();

            SpotifyUser oSpotifyUser = this._oSCCManager.AddSpotifyUser(oPrivateProfile, code, oToken);
            // ViewBag.RedirectUrl = string.Format("/SpotifyChangeControl/Change/Index?UserGuid={0}&SessionGuid={1}", oSpotifyUser.UserGuid, oSpotifyUser.SessionGuid);

            if (!this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("UserGuid"))
            {
                HttpCookie hcUserGuid = new HttpCookie("UserGuid");
                hcUserGuid.Value = oSpotifyUser.UserGuid;
                this.ControllerContext.HttpContext.Response.Cookies.Add(hcUserGuid);
                this.ControllerContext.HttpContext.Response.Cookies.Add(hcUserGuid);
            }
            else
            {
                this.ControllerContext.HttpContext.Request.Cookies.Get("UserGuid").Value = oSpotifyUser.UserGuid;
            }

            if (!this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("SessionGuid"))
            {
                HttpCookie hcSessionGuid = new HttpCookie("SessionGuid");
                hcSessionGuid.Value = oSpotifyUser.SessionGuid;
                this.ControllerContext.HttpContext.Response.Cookies.Add(hcSessionGuid);

            }
            else
            {
                this.ControllerContext.HttpContext.Request.Cookies.Get("SessionGuid").Value = oSpotifyUser.SessionGuid;
            }

            return new RedirectResult(string.Format("/SpotifyChangeControl/Change/Index", oSpotifyUser.UserGuid, oSpotifyUser.SessionGuid));
        }