コード例 #1
0
        private void FetchUserInfo()
        {
            if (authState.AuthorizationServiceConfiguration == null)
            {
                Console.WriteLine("Cannot make userInfo request without service configuration");
            }

            authState.PerformActionWithFreshTokens(authService, async(accessToken, idToken, ex) =>
            {
                if (ex != null)
                {
                    Console.WriteLine("Token refresh failed when fetching user info");
                    return;
                }

                AuthorizationServiceDiscovery discoveryDoc = GetDiscoveryDocFromIntent(Intent);
                if (discoveryDoc == null)
                {
                    throw new InvalidOperationException("no available discovery doc");
                }

                try
                {
                    using (var client = new HttpClient())
                    {
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                        var response = await client.GetStringAsync(discoveryDoc.UserinfoEndpoint.ToString());

                        new Handler(MainLooper).Post(() =>
                        {
                            userInfoJson = new JSONObject(response);
                            RefreshUi();
                        });
                    }
                }
                catch (HttpRequestException ioEx)
                {
                    Console.WriteLine("Network error when querying userinfo endpoint:" + ioEx);
                }
                catch (JSONException jsonEx)
                {
                    Console.WriteLine("Failed to parse userinfo response: " + jsonEx);
                }
            });
        }