Example #1
0
        // Gets an access token. Returns the access token, access token
        // expiration, and refresh token.
        private static AccessTokens GetAccessTokens(string uri)
        {
            var          responseSerializer = new DataContractJsonSerializer(typeof(AccessTokens));
            AccessTokens tokenResponse      = null;

            try
            {
                var realUri = new Uri(uri, UriKind.Absolute);
                var addy    = realUri.AbsoluteUri.Substring(0, realUri.AbsoluteUri.Length - realUri.Query.Length);
                var request = (HttpWebRequest)WebRequest.Create(addy);
                request.Method      = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                using (var writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(realUri.Query.Substring(1));
                }
                var response = (HttpWebResponse)request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                    {
                        tokenResponse = (AccessTokens)responseSerializer.ReadObject(responseStream);
                    }
                }
            }
            catch (WebException e)
            {
                var response = (HttpWebResponse)e.Response;
                Console.WriteLine("HTTP status code: " + response.StatusCode);
            }
            return(tokenResponse);
        }
Example #2
0
        static void Main()
        {
            var uri = string.Format(ConsentUriFormatter, AuthorizeUri, ClientId, DesktopUri);

            _form = new LouiesBingOAuthAutomation(uri);
            if (string.IsNullOrEmpty(_code))
            {
                Console.WriteLine(_error);
                return;
            }
            uri = string.Format(AccessUriFormatter, TokenUri, ClientId, _code, DesktopUri);
            AccessTokens tokens = GetAccessTokens(uri);

            Console.WriteLine("Access token expires in {0} minutes: ", tokens.ExpiresIn / 60);
            Console.WriteLine("\nAccess token: " + tokens.AccessToken);
            Console.WriteLine("\nRefresh token: " + tokens.RefreshToken);
            uri    = string.Format(RefreshUriFormatter, TokenUri, ClientId, DesktopUri, tokens.RefreshToken);
            tokens = GetAccessTokens(uri);
            Console.WriteLine("Access token expires in {0} minutes: ", tokens.ExpiresIn / 60);
            Console.WriteLine("\nAccess token: " + tokens.AccessToken);
            Console.WriteLine("\nRefresh token: " + tokens.RefreshToken);
        }