Exemplo n.º 1
0
        /// <summary>
        /// Get The Facebook User Profile of the Current User
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <param name="access_token">
        /// The access_token.
        /// </param>
        /// <returns>
        /// Returns the FacebookUser Profile
        /// </returns>
        public FacebookUser GetFacebookUser(HttpRequest request, string access_token)
        {
            var url =
                $"https://graph.facebook.com/v3.3/me?fields=email,first_name,last_name,location,birthday,gender,name,link&access_token={access_token}";

            return(AuthUtilities.WebRequest(AuthUtilities.Method.GET, url, string.Empty).FromJson <FacebookUser>());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the access token.
        /// </summary>
        /// <param name="authorizationCode">
        /// The authorization code.
        /// </param>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// Returns the Access Token
        /// </returns>
        public GoogleTokens GetAccessToken(string authorizationCode, HttpRequest request)
        {
            var code = $"code={HttpUtility.UrlEncode(authorizationCode)}";

            return
                (AuthUtilities.WebRequest(
                     AuthUtilities.Method.POST,
                     "https://www.googleapis.com/oauth2/v4/token",
                     $"{code}&client_id={Config.GoogleClientID}&client_secret={Config.GoogleClientSecret}&redirect_uri={HttpUtility.UrlEncode(GetRedirectURL(request))}&grant_type=authorization_code").FromJson <GoogleTokens>());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the access token.
        /// </summary>
        /// <param name="authorizationCode">
        /// The authorization code.
        /// </param>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// Returns the Access Token
        /// </returns>
        public string GetAccessToken(string authorizationCode, HttpRequest request)
        {
            var urlGetAccessToken =
                $"https://graph.facebook.com/v3.3/oauth/access_token?client_id={Config.FacebookAPIKey}&client_secret={Config.FacebookSecretKey}&redirect_uri={GetRedirectURL(request)}&code={authorizationCode}";

            var responseData = AuthUtilities.WebRequest(AuthUtilities.Method.GET, urlGetAccessToken, null);

            if (responseData.IsNotSet())
            {
                return(string.Empty);
            }

            return(responseData.FromJson <FacebookAccessToken>().AccessToken ?? string.Empty);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the access token.
        /// </summary>
        /// <param name="authorizationCode">
        /// The authorization code.
        /// </param>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// Returns the Access Token
        /// </returns>
        public GoogleTokens GetAccessToken(string authorizationCode, HttpRequest request)
        {
            var code = "code={0}".FormatWith(HttpUtility.UrlEncode(authorizationCode));

            return
                (AuthUtilities.WebRequest(
                     AuthUtilities.Method.POST,
                     "https://accounts.google.com/o/oauth2/token",
                     "{0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}".FormatWith(
                         code,
                         Config.GoogleClientID,
                         Config.GoogleClientSecret,
                         HttpUtility.UrlEncode(this.GetRedirectURL(request)),
                         "authorization_code")).FromJson <GoogleTokens>());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get The Google User Profile of the Current User
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <param name="access_token">
        /// The access_token.
        /// </param>
        /// <returns>
        /// Returns the GoogleUser Profile
        /// </returns>
        public GoogleUser GetGoogleUser(HttpRequest request, string access_token)
        {
            var headers = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(
                    "Authorization",
                    $"OAuth {access_token}")
            };

            return
                (AuthUtilities.WebRequest(
                     AuthUtilities.Method.GET,
                     "https://www.googleapis.com/oauth2/v2/userinfo?alt=json",
                     string.Empty,
                     headers).FromJson <GoogleUser>());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the access token.
        /// </summary>
        /// <param name="authorizationCode">
        /// The authorization code.
        /// </param>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// Returns the Access Token
        /// </returns>
        public string GetAccessToken(string authorizationCode, HttpRequest request)
        {
            var urlGetAccessToken =
                "https://graph.facebook.com/v2.9/oauth/access_token?client_id={0}&client_secret={1}&redirect_uri={2}&code={3}&grant_type=client_credentials"
                .FormatWith(
                    Config.FacebookAPIKey,
                    Config.FacebookSecretKey,
                    HttpUtility.UrlEncode(GetRedirectURL(request)),
                    authorizationCode);


            var responseData = AuthUtilities.WebRequest(AuthUtilities.Method.GET, urlGetAccessToken, null);

            if (responseData.IsNotSet())
            {
                return(string.Empty);
            }

            return(responseData.FromJson <FacebookAccessToken>().AccessToken ?? string.Empty);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the access token.
        /// </summary>
        /// <param name="authorizationCode">
        /// The authorization code.
        /// </param>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// Returns the Access Token
        /// </returns>
        public string GetAccessToken(string authorizationCode, HttpRequest request)
        {
            var urlGetAccessToken =
                "https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&redirect_uri={2}&code={3}"
                .FormatWith(
                    Config.FacebookAPIKey,
                    Config.FacebookSecretKey,
                    GetRedirectURL(request),
                    authorizationCode);

            var responseData = AuthUtilities.WebRequest(AuthUtilities.Method.GET, urlGetAccessToken, null);

            if (responseData.IsNotSet())
            {
                return(string.Empty);
            }

            var queryStringCollection = HttpUtility.ParseQueryString(responseData);

            return(queryStringCollection["access_token"] ?? string.Empty);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Submit a web request using oAUTH.
        /// </summary>
        /// <param name="method">
        /// GET or POST
        /// </param>
        /// <param name="url">
        /// The full url, including the query string.
        /// </param>
        /// <param name="postData">
        /// Data to post (query string format)
        /// </param>
        /// <returns>
        /// The web server response.
        /// </returns>
        public string OAuthWebRequest(AuthUtilities.Method method, string url, string postData)
        {
            string outUrl;
            string querystring;

            // Setup postData for signing.
            // Add the postData to the querystring.
            if (method == AuthUtilities.Method.POST)
            {
                if (postData.Length > 0)
                {
                    // Decode the parameters and re-encode using the oAuth UrlEncode method.
                    var qs = HttpUtility.ParseQueryString(postData);
                    postData = string.Empty;
                    foreach (var key in qs.AllKeys)
                    {
                        if (postData.Length > 0)
                        {
                            postData += "&";
                        }

                        qs[key]   = HttpUtility.UrlDecode(qs[key]);
                        qs[key]   = this.UrlEncode(qs[key]);
                        postData += "{0}={1}".FormatWith(key, qs[key]);
                    }

                    if (url.IndexOf("?") > 0)
                    {
                        url += "&";
                    }
                    else
                    {
                        url += "?";
                    }

                    url += postData;
                }
            }
            else if (method == AuthUtilities.Method.GET && !string.IsNullOrEmpty(postData))
            {
                url += "?{0}".FormatWith(postData);
            }

            var uri = new Uri(url);

            var nonce     = this.GenerateNonce();
            var timeStamp = this.GenerateTimeStamp();

            // Generate Signature
            var sig = this.GenerateSignature(
                uri,
                this.ConsumerKey,
                this.ConsumerSecret,
                this.Token,
                this.TokenSecret,
                this.CallBackUrl,
                method.ToString(),
                timeStamp,
                nonce,
                this.PIN,
                out outUrl,
                out querystring);

            querystring += "&oauth_signature={0}".FormatWith(HttpUtility.UrlEncode(sig));

            // Convert the querystring to postData
            if (method == AuthUtilities.Method.POST)
            {
                postData    = querystring;
                querystring = string.Empty;
            }

            if (querystring.Length > 0)
            {
                outUrl += "?";
            }

            var ret = AuthUtilities.WebRequest(method, "{0}{1}".FormatWith(outUrl, querystring), postData);

            return(ret);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Get The Facebook User Profile of the Current User
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <param name="access_token">
        /// The access_token.
        /// </param>
        /// <returns>
        /// Returns the FacebookUser Profile
        /// </returns>
        public FacebookUser GetFacebookUser(HttpRequest request, string access_token)
        {
            var url = "https://graph.facebook.com/me?access_token={0}".FormatWith(access_token);

            return(AuthUtilities.WebRequest(AuthUtilities.Method.GET, url, string.Empty).FromJson <FacebookUser>());
        }
Exemplo n.º 10
0
        /// <summary>
        /// Submit a web request using oAUTH.
        /// </summary>
        /// <param name="method">
        /// GET or POST
        /// </param>
        /// <param name="url">
        /// The full url, including the query string.
        /// </param>
        /// <param name="postData">
        /// Data to post (query string format)
        /// </param>
        /// <returns>
        /// The web server response.
        /// </returns>
        public string OAuthWebRequest(AuthUtilities.Method method, string url, string postData)
        {
            // Setup postData for signing.
            // Add the postData to the querystring.
            if (method == AuthUtilities.Method.POST)
            {
                if (postData.Length > 0)
                {
                    // Decode the parameters and re-encode using the oAuth UrlEncode method.
                    var qs = HttpUtility.ParseQueryString(postData);
                    postData = string.Empty;

                    qs.AllKeys.ForEach(
                        key =>
                    {
                        if (postData.Length > 0)
                        {
                            postData += "&";
                        }

                        qs[key]   = HttpUtility.UrlDecode(qs[key]);
                        qs[key]   = this.UrlEncode(qs[key]);
                        postData += $"{key}={qs[key]}";
                    });

                    if (url.IndexOf("?", StringComparison.Ordinal) > 0)
                    {
                        url += "&";
                    }
                    else
                    {
                        url += "?";
                    }

                    url += postData;
                }
            }
            else if (method == AuthUtilities.Method.GET && postData.IsSet())
            {
                url += $"?{postData}";
            }

            var uri = new Uri(url);

            var nonce     = this.GenerateNonce();
            var timeStamp = this.GenerateTimeStamp();

            // Generate Signature
            var sig = this.GenerateSignature(
                uri,
                this.ConsumerKey,
                this.ConsumerSecret,
                this.Token,
                this.TokenSecret,
                this.CallBackUrl,
                method.ToString(),
                timeStamp,
                nonce,
                this.PIN,
                out var outUrl,
                out var querystring);

            querystring += $"&oauth_signature={HttpUtility.UrlEncode(sig)}";

            // Convert the querystring to postData
            if (method == AuthUtilities.Method.POST)
            {
                postData    = querystring;
                querystring = string.Empty;
            }

            if (querystring.Length > 0)
            {
                outUrl += "?";
            }

            var ret = AuthUtilities.WebRequest(method, $"{outUrl}{querystring}", postData);

            return(ret);
        }