예제 #1
0
        public void TestOAuthDesktopFlow()
        {
            using (var flow = new OAuthDesktopFlow())
            {
                var oauthLink = new OAuth2AuthorizeLink();

                var token = flow.GetAuthCode(oauthLink.Target, new Uri("http://localhost:9696/"));
                Thread.Sleep(5000);
                Assert.NotNull(token);
            }
        }
        public string GetAuthToken()
        {
            var redirectUri = "http://localhost:9696/";
            var oauthLink = new OAuth2AuthorizeLink();
            string authcode;
            using (var flow = new OAuthDesktopFlow())
            {
                authcode = flow.GetAuthCode(oauthLink.Target, new Uri(redirectUri));
                Thread.Sleep(3000);  // Let web browser render page
            }

            var tokenLink = new OAuth2TokenLink();

            var request = tokenLink.BuildRequest(new Dictionary<string, string>()
            {
                {"client_id", "1acc6a2e-dd01-49ad-a856-e24789c77529"},
                {"client_secret", "42ebcddd-6988-4835-895c-cff19bdbdbe9"},
                {"code", authcode},
                {"grant_type", "authorization_code"},
                {"redirect_uri", redirectUri}
            });

            var response = _httpClient.SendAsync(request).Result;
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var token = OAuth2TokenLink.ParseTokenBody(response.Content.ReadAsStringAsync().Result);
                return token.AccessToken;
            }
            else
            {
                var error = OAuth2TokenLink.ParseErrorBody(response.Content.ReadAsStringAsync().Result);
                throw new Exception("Failed to obtain token : " + error.ErrorDescription);
            }

        }