/// <summary> /// Http Put with json content /// </summary> /// <param name="url"></param> /// <param name="body"></param> private async void httpPut(string url, string body) { try { var request = new HttpRequestMessage() { Method = HttpMethod.Put, RequestUri = new Uri(url), Content = new StringContent(body, Encoding.UTF8, "application/json") }; request.Headers.Authorization = new AuthenticationHeaderValue(_oAuth2Response.token_type, _oAuth2Response.access_token); var response = await httpClient.SendAsync(request); return; } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse HttpWebResponse = (HttpWebResponse)ex.Response; if (HttpWebResponse.StatusCode == HttpStatusCode.Unauthorized) { _oAuth2Response = Client.GetToken(_oAuth2Request, "refresh_token", _oAuth2Response.refresh_token); } } } }
/// <summary> /// spotify認証ボタン /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpenSpotifyAuthorization_Click(object sender, EventArgs e) { using (Client c = new Client(_spotifyoAuth2Request)) { c.ShowDialog(this); _spotifyoAuth2Response = c.oAuth2Response; c.Dispose(); } if (_spotifyoAuth2Response != null) { //設定保存 Properties.Settings.Default.Spotify_Refresh_token = _spotifyoAuth2Response.refresh_token; Properties.Settings.Default.Spotify_Access_token = _spotifyoAuth2Response.access_token; Properties.Settings.Default.Save(); } }
/// <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; } } }
private void wb1_Navigated(object sender, WebBrowserNavigatedEventArgs e) { string url = wb1.Url.AbsoluteUri; //token取得 if (url.StartsWith(this._oAuth2Request.redirect_uri)) { string s = url.Replace(string.Format("{0}?", this._oAuth2Request.redirect_uri), ""); string[] v = s.Split('&'); for (int i = 0; i <= v.Length - 1; i++) { string[] x = v[i].Split('='); switch (x[0]) { case "code": res.code = x[1]; break; case "error": res.error = x[1]; break; case "state": res.state = x[1]; break; } } //todo キャンセルイベント //codeありでstateが一致する場合だけ if (res.code != null && res.state == _oAuth2Request.state) { _oAuth2Response = GetToken(_oAuth2Request, "authorization_code", res.code); //画面閉じる Close(); } } }
/// <summary> /// json取得 /// </summary> /// <param name="url">endpoint</param> /// <returns></returns> private string getJson(string url) { if (_oAuth2Response == null) { return(""); } using (WebClient webClient = new WebClient()) { webClient.Headers.Add(HttpRequestHeader.Accept, " application/json"); webClient.Headers.Add(HttpRequestHeader.ContentType, " application/json"); webClient.Encoding = Encoding.UTF8; webClient.Headers.Add(HttpRequestHeader.Authorization, string.Format("{0} {1}", _oAuth2Response.token_type, _oAuth2Response.access_token)); string json = ""; try { json = webClient.DownloadString(url); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse HttpWebResponse = (HttpWebResponse)ex.Response; if (HttpWebResponse.StatusCode == HttpStatusCode.Unauthorized) { _oAuth2Response = Client.GetToken(_oAuth2Request, "refresh_token", _oAuth2Response.refresh_token); } } //break; } catch (Exception) { //break; } return(json); } }
/// <summary> /// 接続設定 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmConntctionSettings_Click(object sender, System.EventArgs e) { try { using (dlgConnectionSettings c = new dlgConnectionSettings() { spotifyoAuth2Request = this.spotifyoAuth2Request, spotifyoAuth2Response = this.spotifyoAuth2Response }) { c.ShowDialog(this); spotifyoAuth2Response = c.spotifyoAuth2Response; c.Dispose(); } } finally { //mastodon接続設定 initMastodonSettings(); //spotify接続設定 initSpotifySettings(); if (spotifyoAuth2Request != null && spotifyoAuth2Response != null) { spotifyApi = new Dispatcher(spotifyoAuth2Request, spotifyoAuth2Response); } if (spotifyApi == null) { tpsSpotifyApiStatus.Text = "UnAuthorized."; } else { tpsSpotifyApiStatus.Text = "Connected."; //曲情報を更新 setCurrentPlayback(100); } } }
/// <summary> /// コンストラクタ /// </summary> /// <param name="req"></param> /// <param name="res"></param> public Dispatcher(oAuth2Request req, oAuth2Response res) { _oAuth2Request = req; _oAuth2Response = res; }