/// <summary>
        /// Occurs when user gets authorized.
        /// </summary>
        /// <param name="userId">User Id.</param>
        /// <param name="result">Result.</param>
        /// <returns>Result.</returns>
        protected override OAuthAuthorizationResult OnAuthorized(int userId, OAuthAuthorizationResult result)
        {
            OAuthAuthorizationResult ret = result;
            string ping = "https://www.googleapis.com/analytics/v3/data/ga";

            if (!OAuthWebClient.CheckAuthorized(ping, result.AccessToken))
            {
                ret = EnsureAuthorization(userId, true);

                if (ret == null || string.IsNullOrEmpty(ret.AccessToken) ||
                    !OAuthWebClient.CheckAuthorized(ping, ret.AccessToken))
                {
                    ret.Authorized  = false;
                    ret.AccessToken = string.Empty;
                }
            }

            return(ret);
        }
예제 #2
0
            /// <summary>
            /// Checks whether the given client is allowed to call the given URL.
            /// </summary>
            /// <param name="url">URL.</param>
            /// <param name="accessToken">Access token.</param>
            /// <returns>Value indicating whether the given client is allowed to call the given URL.</returns>
            public static bool CheckAuthorized(string url, string accessToken)
            {
                bool ret = true;

                try
                {
                    using (var client = new OAuthWebClient(accessToken))
                        client.DownloadString(url);
                }
                catch (System.Net.WebException ex)
                {
                    if (ex.Response != null && (ex.Response as System.Net.HttpWebResponse).StatusCode ==
                        System.Net.HttpStatusCode.Unauthorized)
                    {
                        ret = false;
                    }
                }

                return(ret);
            }