Beispiel #1
0
    public OAuthParameters BuildProtectedResourceSignature(string method, WebPairCollection parameters, string url)
    {
        ValidateProtectedResourceState();

        var allParameters = new WebPairCollection();

        allParameters.AddRange(parameters);

        // Include url parameters in query pool
        var uri           = new Uri(url);
        var urlParameters = HttpUtility.ParseQueryString(uri.Query);

        allParameters.AddRange(urlParameters.AllKeys.Select(x => new WebPair(x !, urlParameters[x] !)));

        var timestamp = OAuthTools.GetTimestamp();
        var nonce     = OAuthTools.GetNonce();

        var authParameters = GenerateAuthParameters(timestamp, nonce);

        allParameters.AddRange(authParameters);

        var signatureBase = OAuthTools.ConcatenateRequestElements(method, url, allParameters);

        return(new OAuthParameters {
            Signature = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret, TokenSecret),
            Parameters = authParameters
        });
    }
Beispiel #2
0
        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);
        }
Beispiel #3
0
    /// <summary>
    /// Generates an OAuth signature to pass to an
    /// <see cref="IAuthenticator" /> for the purpose of requesting an
    /// unauthorized request token.
    /// </summary>
    /// <param name="method">The HTTP method for the intended request</param>
    /// <param name="parameters">Any existing, non-OAuth query parameters desired in the request</param>
    /// <returns></returns>
    public OAuthParameters BuildRequestTokenInfo(string method, WebPairCollection parameters)
    {
        ValidateTokenRequestState();

        var allParameters = new WebPairCollection();

        allParameters.AddRange(parameters);

        var timestamp = OAuthTools.GetTimestamp();
        var nonce     = OAuthTools.GetNonce();

        var authParameters = GenerateAuthParameters(timestamp, nonce);

        allParameters.AddRange(authParameters);

        var signatureBase = OAuthTools.ConcatenateRequestElements(method, Ensure.NotNull(RequestTokenUrl, nameof(RequestTokenUrl)), allParameters);

        return(new OAuthParameters {
            Signature = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret),
            Parameters = authParameters
        });
    }
Beispiel #4
0
    /// <summary>
    /// Generates an OAuth signature to pass to an
    /// <see cref="IAuthenticator" /> for the purpose of exchanging user credentials
    /// for an access token authorized by the user at the Service Provider site.
    /// </summary>
    /// <param name="method">The HTTP method for the intended request</param>
    /// <param name="parameters">Any existing, non-OAuth query parameters desired in the request</param>
    public OAuthParameters BuildClientAuthAccessTokenSignature(string method, WebPairCollection parameters)
    {
        ValidateClientAuthAccessRequestState();

        var allParameters = new WebPairCollection();

        allParameters.AddRange(parameters);

        var uri       = new Uri(Ensure.NotNull(AccessTokenUrl, nameof(AccessTokenUrl)));
        var timestamp = OAuthTools.GetTimestamp();
        var nonce     = OAuthTools.GetNonce();

        var authParameters = GenerateXAuthParameters(timestamp, nonce);

        allParameters.AddRange(authParameters);

        var signatureBase = OAuthTools.ConcatenateRequestElements(method, uri.ToString(), allParameters);

        return(new OAuthParameters {
            Signature = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret),
            Parameters = authParameters
        });
    }
Beispiel #5
0
        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();
                }
            });
        }