private async Task Authenticate() { if (!string.IsNullOrEmpty(_token)) { return; } if (CredentialStore.ObjectExists("google.auth.json")) { var access = CredentialStore.RetrieveObject("google.auth.json"); if (DateTime.UtcNow >= DateTime.Parse((string)access.expiry)) { access = await RefreshAccessToken(access); StoreAccess(access); } _token = access.access_token; } else { var access = await GetNewAccessToken(); StoreAccess(access); _token = access.access_token; } }
private dynamic CreateProxy() { var client = new RestClient("http://dev.virtualearth.net/REST/v1/"); client.AddDefaultParameter("key", CredentialStore.Key("bing")); return(new RestProxy(client)); }
public async Task GetMethod2PathAsProperty2Params() { var client = new RestClient("http://openstates.org/api/v1"); client.AddDefaultHeader("X-APIKEY", CredentialStore.Key("sunlight")); dynamic proxy = new RestProxy(client); var result = await proxy.legislators.geo.get(lat : 44.926868, _long : -93.214049); Assert.IsNotNull(result); Assert.IsTrue(result.Count > 0); }
public async Task GetMethodSegmentWithArgs() { var client = new RestClient("http://openstates.org/api/v1"); client.AddDefaultHeader("X-APIKEY", CredentialStore.Key("sunlight")); dynamic proxy = new RestProxy(client); var result = await proxy.bills.mn("2013s1")("SF 1").get(); Assert.IsNotNull(result); Assert.IsTrue(result.id == "MNB00017167"); }
public async Task ExplicitGetInvoke() { var client = new RestClient("http://openstates.org/api/v1"); client.AddDefaultHeader("X-APIKEY", CredentialStore.Key("sunlight")); dynamic proxy = new RestProxy(client); dynamic result = await proxy.metadata.mn.get(); Assert.IsNotNull(result); Assert.IsTrue(result.name == "Minnesota"); }
public async Task GetMethod1PathArg1Param() { var client = new RestClient("http://openstates.org/api/v1"); client.AddDefaultHeader("X-APIKEY", CredentialStore.Key("sunlight")); dynamic proxy = new RestProxy(client); var result = await proxy.bills.get(state : "mn", chamber : "upper", status : "passed_upper"); Assert.IsNotNull(result); Assert.IsNotNull(result.Count > 0); Assert.IsTrue(result[0].chamber == "upper"); }
private async Task <dynamic> RefreshAccessToken(dynamic access) { Assert.IsNotNull((string)access.refresh_token); dynamic key = CredentialStore.JsonKey("google").installed; var client = new RestClient("https://accounts.google.com"); dynamic proxy = new RestProxy(client); var response = await proxy.o.oauth2.token.post(client_id : key.client_id, client_secret : key.client_secret, refresh_token : access.refresh_token, grant_type : "refresh_token"); Assert.IsNotNull(response); return(response); }
private async Task <dynamic> GetNewAccessToken() { dynamic key = CredentialStore.JsonKey("google").installed; var client = new RestClient("https://accounts.google.com"); dynamic proxy = new RestProxy(client); var response = await proxy.o.oauth2.device.code.post(client_id : key.client_id, scope : "email profile https://www.googleapis.com/auth/calendar"); Assert.IsNotNull(response); Debug.WriteLine((string)response.user_code); // use clip.exe to put the user code on the clipboard Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = string.Format("/c echo {0} | clip", response.user_code); p.Start(); // this requires user permission - open a broswer - enter the user_code which is now in the clipboard Process.Start((string)response.verification_url); int expiration = response.expires_in; int interval = response.interval; int time = interval; dynamic tokenResonse = null; // we are using the device flow so enter the code in the browser - poll google for success while (time < expiration) { Thread.Sleep(interval * 1000); tokenResonse = await proxy.o.oauth2.token.post(client_id : key.client_id, client_secret : key.client_secret, code : response.device_code, grant_type : "http://oauth.net/grant_type/device/1.0"); if (tokenResonse.access_token != null) { break; } time += interval; } Assert.IsNotNull(tokenResonse); return(tokenResonse); }
private static void StoreAccess(dynamic access) { access.expiry = DateTime.UtcNow.Add(TimeSpan.FromSeconds((int)access.expires_in)); CredentialStore.StoreObject("google.auth.json", access); }