protected async Task <TReturn> ExecuteNoReturn <TReturn>(HttpMethod method, string logicMethod, Dictionary <string, string> param, string mappingName, Func <FlickrApiResponse <object>, TReturn> selector) { try { var response = new HttpRequestMessage(method, GetMethodDescription(logicMethod, param)); var res = await Client.SendAsync(response); if (res.StatusCode == HttpStatusCode.Unauthorized) { await IdentityProvider.ReAuthorize(); } if (!res.IsSuccessStatusCode) { throw new Exception(); } var json = await res.Content.ReadAsStringAsync(); json = json.Substring("jsonFlickrApi(".Length, json.Length - "jsonFlickrApi(".Length - 1); var temp = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(json); var apiResponse = new FlickrApiResponse <object> { Success = (string)temp.First(pair => pair.Key == "stat").Value }; return(selector(apiResponse)); } catch (Exception e) { Debug.WriteLine(e); return(default(TReturn)); } }
public async Task <Uri> GetAuthorisationUrl() { Frob = null; try { var meth = $"?method=flickr.auth.getFrob&api_key={FlickrBaseClient.ClientId}&format=json&api_sig={Md5($"{SecretKey}api_key{FlickrBaseClient.ClientId}formatjsonmethodflickr.auth.getFrob")}"; var response = new HttpRequestMessage(HttpMethod.Get, meth); var res = await Client.SendAsync(response); if (!res.IsSuccessStatusCode) { throw new Exception(); } var json = await res.Content.ReadAsStringAsync(); json = json.Substring("jsonFlickrApi(".Length, json.Length - "jsonFlickrApi(".Length - 1); var temp = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(json); var apiResponse = new FlickrApiResponse <JObject> { Data = ((JObject)temp.First().Value).ToObject <JObject>(), Success = (string)temp.First(pair => pair.Key == "stat").Value }; Frob = apiResponse.Data.Value <string>("_content"); var signature = Md5($"{SecretKey}api_key{FlickrBaseClient.ClientId}frob{Frob}permswrite"); return(new Uri($"http://www.flickr.com/services/auth/?api_key={FlickrBaseClient.ClientId}&perms=write&frob={Frob}&api_sig={signature}")); } catch (Exception e) { Debug.WriteLine(e); return(null); } }
public async Task Authorize(string pin) { try { var signature = Md5($"{SecretKey}api_key{FlickrBaseClient.ClientId}formatjsonfrob{Frob}methodflickr.auth.getToken"); var meth = $"?method=flickr.auth.getToken&api_key={FlickrBaseClient.ClientId}&frob={Frob}&format=json&api_sig={signature}"; var response = new HttpRequestMessage(HttpMethod.Get, meth); var res = await Client.SendAsync(response); if (!res.IsSuccessStatusCode) { throw new Exception(); } var json = await res.Content.ReadAsStringAsync(); json = json.Substring("jsonFlickrApi(".Length, json.Length - "jsonFlickrApi(".Length - 1); var temp = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(json); var apiResponse = new FlickrApiResponse <FlickrAuthorisation> { Data = ((JObject)temp.First().Value).ToObject <FlickrAuthorisation>(), Success = (string)temp.First(pair => pair.Key == "stat").Value }; IdentityToken = apiResponse.Data.Token.Value; UserId = apiResponse.Data.AuthenticatedUser.Nsid; UserName = apiResponse.Data.AuthenticatedUser.Username; } catch (Exception e) { Debug.WriteLine(e); } }