private void getCar2GoAccounts(string token, string token_secret, DownloadStringCompletedEventHandler requestCallback) { var car2GoRequestEndpoint = "https://www.car2go.com/api/v2.1/accounts"; var parameters = new WebParameterCollection(); parameters.Add("oauth_callback", "oob"); parameters.Add("oauth_signature_method", "HMAC-SHA1"); parameters.Add("oauth_token", token); parameters.Add("oauth_version", "1.0"); parameters.Add("oauth_consumer_key", FreeCarsCredentials.Car2Go.ConsumerKey); parameters.Add("oauth_timestamp", OAuthTools.GetTimestamp()); parameters.Add("oauth_nonce", OAuthTools.GetNonce()); parameters.Add("format", "json"); parameters.Add("loc", Car2Go.City); //parameters.Add("test", "1"); var signatureBase = OAuthTools.ConcatenateRequestElements("GET", car2GoRequestEndpoint, parameters); var signature = OAuthTools.GetSignature(OAuthSignatureMethod.HmacSha1, OAuthSignatureTreatment.Escaped, signatureBase, FreeCarsCredentials.Car2Go.SharedSecred, token_secret); var requestParameters = OAuthTools.NormalizeRequestParameters(parameters); var requestUrl = new Uri(car2GoRequestEndpoint + "?" + requestParameters + "&oauth_signature=" + signature, UriKind.Absolute); var webClient = new WebClient(); webClient.DownloadStringCompleted += requestCallback; webClient.DownloadStringAsync(requestUrl); }
public void NormalizeRequestParameters_Normalizes_And_Encodes_QueryString() { var parameters = new WebParameterCollection(new[] { new WebParameter("test", "value"), new WebParameter("name", "joe"), new WebParameter("grant_type", "code"), new WebParameter("string", "this+is+a+string"), }); var expected = "grant_type=code&name=joe&string=this%2Bis%2Ba%2Bstring&test=value"; var result = OAuthTools.NormalizeRequestParameters(parameters); Assert.Equal(expected, result); }
public void Can_sort_and_normalize_parameters() { var input = new WebParameterCollection { { "a", "1" }, { "f", "50" }, { "f", "25" }, { "z", "t" }, { "f", "a" }, { "c", "hi there" }, { "z", "p" }, }; const string expected = "a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t"; var actual = OAuthTools.NormalizeRequestParameters(input); Console.WriteLine(actual); Assert.AreEqual(expected, actual); }
public void NormalizeRequestParameters_Throws_ArgumentNull_On_Null_Collection() { Assert.Throws <ArgumentNullException>(() => OAuthTools.NormalizeRequestParameters(null)); }
public void NormalizeRequestParameters_Returns_Blank_On_Empty_Collection() { var result = OAuthTools.NormalizeRequestParameters(new WebParameterCollection()); Assert.Equal(String.Empty, result); }
private void LoadBookedCars() { HasBooking = false; try { var token = (string)App.GetAppSetting("car2go.oauth_token"); var tokenSecret = (string)App.GetAppSetting("car2go.oauth_token_secret"); if (null == token || null == tokenSecret) { return; } const string car2GoRequestEndpoint = "https://www.car2go.com/api/v2.1/booking"; var parameters = new WebParameterCollection { { "oauth_callback", "oob" }, { "oauth_signature_method", "HMAC-SHA1" }, { "oauth_token", token }, { "oauth_version", "1.0" }, { "oauth_consumer_key", consumerkey }, { "oauth_timestamp", OAuthTools.GetTimestamp() }, { "oauth_nonce", OAuthTools.GetNonce() }, { "format", "json" }, { "loc", City }, }; //parameters.Add("test", "1"); var signatureBase = OAuthTools.ConcatenateRequestElements("GET", car2GoRequestEndpoint, parameters); var signature = OAuthTools.GetSignature(OAuthSignatureMethod.HmacSha1, OAuthSignatureTreatment.Escaped, signatureBase, FreeCarsCredentials.Car2Go.SharedSecred, tokenSecret); var requestParameters = OAuthTools.NormalizeRequestParameters(parameters); var requestUrl = new Uri(car2GoRequestEndpoint + "?" + requestParameters + "&oauth_signature=" + signature, UriKind.Absolute); var webClient = new WebClient(); webClient.OpenReadCompleted += (sender, args) => { try { if (0 == args.Result.Length) { return; } try { var serializer = new DataContractJsonSerializer(typeof(Car2GoBookingResult)); var bookingResult = (Car2GoBookingResult)serializer.ReadObject(args.Result); var car2GoCars = new List <Car2GoMarker>(); if (0 == bookingResult.ReturnValue.Code) { if (bookingResult.Booking.Length > 0) { lastBookedCarsUpdate = DateTime.Now; } foreach (var booking in bookingResult.Booking) { var car = booking.Vehicle; GeoCoordinate carPosition = null; try { carPosition = new GeoCoordinate(car.Position.Latitude, car.Position.Longitude); } catch {} var carInfo = new Car2GoMarker { model = ("CE" == car.EngineType) ? "C-Smart" : "Smart ElectricDrive", fuelState = car.Fuel, position = carPosition, licensePlate = car.NumberPlate, ID = car.VIN, exterior = car.Exterior, interior = car.Interior, isBooked = true, BookingId = bookingResult.Booking[0].BookingId, }; HasBooking = true; car2GoCars.Add(carInfo); } Markers = car2GoCars; if (null != Updated) { Updated(this, null); } } } catch (NullReferenceException) { } } catch (WebException) { } }; webClient.OpenReadAsync(requestUrl); } catch (Exception e) { Console.WriteLine(e); } }
private void CreateCar2GoBooking(DownloadStringCompletedEventHandler requestCallback) { var item = (Car2GoMarker)Item; var car2GoRequestEndpoint = "https://www.car2go.com/api/v2.1/bookings"; var oauth_timestamp = (DateTime?)App.GetAppSetting("car2go.oauth_token_timestamp"); if (null == oauth_timestamp) { App.ClearAppSetting("car2go.oauth_token"); App.ClearAppSetting("car2go.oauth_token_secret"); } else if (((DateTime)oauth_timestamp).AddDays(90.0).CompareTo(DateTime.UtcNow) <= 0) { App.ClearAppSetting("car2go.oauth_token"); App.ClearAppSetting("car2go.oauth_token_secret"); } var oauthToken = (string)App.GetAppSetting("car2go.oauth_token"); var oauthTokenSecret = (string)App.GetAppSetting("car2go.oauth_token_secret"); if (null == oauthToken || null == oauthTokenSecret) { HandleNotConnectedToCar2Go(null == oauth_timestamp ? "" : Strings.SettingsPageCar2GoAuthExpired); } var accountId = ""; try { accountId = ((int)App.GetAppSetting("car2go.oauth_account_id")).ToString(); } catch (NullReferenceException) { return; } var parameters = new WebParameterCollection(); parameters.Add("oauth_callback", "oob"); parameters.Add("oauth_signature_method", "HMAC-SHA1"); parameters.Add("oauth_token", oauthToken); parameters.Add("oauth_version", "1.0"); parameters.Add("oauth_consumer_key", FreeCarsCredentials.Car2Go.ConsumerKey); parameters.Add("oauth_timestamp", OAuthTools.GetTimestamp()); parameters.Add("oauth_nonce", OAuthTools.GetNonce()); parameters.Add("format", "json"); parameters.Add("loc", Car2Go.City); parameters.Add("vin", item.ID); parameters.Add("account", accountId); var signatureBase = OAuthTools.ConcatenateRequestElements("POST", car2GoRequestEndpoint, parameters); var signature = OAuthTools.GetSignature( OAuthSignatureMethod.HmacSha1, OAuthSignatureTreatment.Escaped, signatureBase, FreeCarsCredentials.Car2Go.SharedSecred, oauthTokenSecret); var requestParameters = OAuthTools.NormalizeRequestParameters(parameters); var para = requestParameters + "&oauth_signature=" + signature; Helpers.Post(car2GoRequestEndpoint, para, delegate(Stream response) { if (null == response) return; var serializer = new DataContractJsonSerializer(typeof(Car2GoBookingResult)); var resultAccounts = (Car2GoBookingResult)serializer.ReadObject(response); Dispatcher.BeginInvoke(() => { var mbResult = MessageBoxResult.None; try { mbResult = 0 == resultAccounts.ReturnValue.Code ? MessageBox.Show(resultAccounts.Booking[0].Vehicle.Position.Address, resultAccounts.ReturnValue.Description, MessageBoxButton.OK) : MessageBox.Show(resultAccounts.ReturnValue.Description); } catch (Exception) { Deactivate(); } if (mbResult == MessageBoxResult.OK) { InvokeActionCompleted(); FlurryWP7SDK.Api.LogEvent("Car2GoBookingSucessfull"); } }); }); FlurryWP7SDK.Api.LogEvent("Car2GoookingStarted"); }
private void CancelCar2GoBooking() { var item = (Car2GoMarker)Item; var car2GoRequestEndpoint = "https://www.car2go.com/api/v2.1/booking/" + item.BookingId; var oauthToken = (string)App.GetAppSetting("car2go.oauth_token"); var oauthTokenSecret = (string)App.GetAppSetting("car2go.oauth_token_secret"); if (null == oauthToken || null == oauthTokenSecret) { HandleNotConnectedToCar2Go(); } var accountId = ""; try { accountId = ((int)App.GetAppSetting("car2go.oauth_account_id")).ToString(); } catch (NullReferenceException) { return; } var parameters = new WebParameterCollection(); parameters.Add("oauth_callback", "oob"); parameters.Add("oauth_signature_method", "HMAC-SHA1"); parameters.Add("oauth_token", oauthToken); parameters.Add("oauth_version", "1.0"); parameters.Add("oauth_consumer_key", FreeCarsCredentials.Car2Go.ConsumerKey); parameters.Add("oauth_timestamp", OAuthTools.GetTimestamp()); parameters.Add("oauth_nonce", OAuthTools.GetNonce()); parameters.Add("format", "json"); parameters.Add("account", accountId); var signatureBase = OAuthTools.ConcatenateRequestElements("DELETE", car2GoRequestEndpoint, parameters); var signature = OAuthTools.GetSignature( OAuthSignatureMethod.HmacSha1, OAuthSignatureTreatment.Escaped, signatureBase, FreeCarsCredentials.Car2Go.SharedSecred, oauthTokenSecret); var requestParameters = OAuthTools.NormalizeRequestParameters(parameters); var para = requestParameters + "&oauth_signature=" + signature; Helpers.Delete(car2GoRequestEndpoint, para, delegate(Stream args) { if (null == args) return; try { var serializer = new DataContractJsonSerializer(typeof(Car2GoCancelBookingResult)); var resultAccount = (Car2GoCancelBookingResult)serializer.ReadObject(args); Dispatcher.BeginInvoke(() => { var mbResult = MessageBoxResult.None; try { if (0 == resultAccount.ReturnValue.Code) { var message = (resultAccount.CancelBooking[0].cancelFeeExists) ? String.Format( Strings.BookingPageC2GCancelationSuccessful, resultAccount.CancelBooking[0].cancelFee, resultAccount.CancelBooking[0].cancelFeeCurrency) : String.Format( Strings.BookingPageC2GCancelationSuccessful, 0, ""); mbResult = MessageBox.Show( message, resultAccount.ReturnValue.Description, MessageBoxButton.OK); } else { mbResult = MessageBox.Show(resultAccount.ReturnValue.Description); } } catch (Exception) { Deactivate(); } if (mbResult != MessageBoxResult.OK) { return; } InvokeActionCompleted(); }); } catch (SerializationException) { InvokeActionCompleted(); } }); }