/// <summary> /// 認証コードをtokenに変換 /// </summary> /// <returns></returns> public static oAuth2Response GetToken(oAuth2Request _oAuth2Request, string grant_type, string code) { try { using (WebClient webClient = new WebClient()) { Encoding encoding = Encoding.UTF8; string sBasicAuth = string.Format("Basic {0}", Convert.ToBase64String( encoding.GetBytes(string.Format("{0}:{1}", _oAuth2Request.client_id, _oAuth2Request.client_secret)) )); webClient.Headers.Add(HttpRequestHeader.Authorization, sBasicAuth); webClient.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); webClient.Encoding = Encoding.UTF8; NameValueCollection ps = new NameValueCollection(); ps.Clear(); ps.Add("redirect_uri", _oAuth2Request.redirect_uri); switch (grant_type) { case "authorization_code": ps.Add("grant_type", "authorization_code"); ps.Add("code", code); break; case "refresh_token": ps.Add("grant_type", "refresh_token"); ps.Add("refresh_token", code); break; } byte[] resData = webClient.UploadValues(_oAuth2Request.auth_server_token_endpoint, ps); webClient.Dispose(); return(JsonConvert.DeserializeObject <oAuth2Response>(Encoding.UTF8.GetString(resData))); } } catch (WebException) { //webex. return(null); } }
/// <summary> /// spotify接続設定 /// </summary> private void initSpotifySettings() { if (spotifyoAuth2Request == null) { spotifyoAuth2Request = new oAuth2Request() { auth_server_auth_endpoint = Properties.Settings.Default.Spotify_Endpoint_auth , auth_server_token_endpoint = Properties.Settings.Default.Spotify_Endpoint_token , client_id = Properties.Settings.Default.Spotify_ClientId , client_secret = Properties.Settings.Default.Spotify_ClientSecret , response_type = "code" , redirect_uri = Properties.Settings.Default.Spotify_Redirect_url , scope = Properties.Settings.Default.Spotify_oAuth_scope }; } //接続設定 if (!string.IsNullOrEmpty(Properties.Settings.Default.Spotify_Access_token) && !string.IsNullOrEmpty(Properties.Settings.Default.Spotify_Refresh_token)) { spotifyoAuth2Response = new oAuth2Response() { access_token = Properties.Settings.Default.Spotify_Access_token , refresh_token = Properties.Settings.Default.Spotify_Refresh_token , token_type = "Bearer" }; spotifyApi = new Dispatcher(spotifyoAuth2Request, spotifyoAuth2Response); if (spotifyApi != null) { tpsSpotifyApiStatus.Text = "Connected."; tpsSpotifyApiStatus.ForeColor = Color.Black; } } }
/// <summary> /// コンストラクタ /// </summary> /// <param name="req"></param> /// <param name="res"></param> public Dispatcher(oAuth2Request req, oAuth2Response res) { _oAuth2Request = req; _oAuth2Response = res; }
/// <summary> /// コンストラクタ /// </summary> /// <param name="oAuth2Request"></param> public Client(oAuth2Request oAuth2Request) { InitializeComponent(); _oAuth2Request = oAuth2Request; }