Пример #1
0
        public void HandleAccessTokenResponse(string response)
        {
            QueryParameters responseCollection = new QueryParameters();

            try
            {
                if (response.StartsWith("{")) // access token is returned in JSON format
                {
                    //  {"access_token":"asasdasdAA","expires_in":3600,"scope":"wl.basic","token_type":"bearer"}
                    JObject accessTokenJson = JObject.Parse(response);
                    responseCollection.Add("response", response);
                    ConnectionToken.AccessToken = accessTokenJson.SelectToken("access_token").ToString().Replace("\"", "");
                    if (accessTokenJson.SelectToken("expires_in") != null)
                    {
                        ConnectionToken.ExpiresOn = DateTime.Now.AddSeconds(int.Parse(accessTokenJson.SelectToken("expires_in").ToString().Replace("\"", "")) - 20);
                    }
                    //put in raw list
                    foreach (var t in accessTokenJson.AfterSelf())
                    {
                        ConnectionToken.ResponseCollection.Add(t.Type.ToString(), t.ToString());
                    }
                    logger.Info("Access Token successfully received");
                    isSuccess = true;
                }
                else // access token is returned as part of Query String
                {
                    responseCollection = Utility.GetQuerystringParameters(response);
                    string keyForAccessToken = responseCollection.Single(x => x.Key.Contains("token")).Key;

                    ConnectionToken.AccessToken = responseCollection[keyForAccessToken].Replace("\"", "");
                    if (responseCollection.ToList().Exists(x => x.Key.ToLower().Contains("expir")))
                    {
                        string keyForExpiry = responseCollection.Single(x => x.Key.Contains("expir")).Key;
                        ConnectionToken.ExpiresOn = ConnectionToken.ExpiresOn = DateTime.Now.AddSeconds(int.Parse(responseCollection[keyForExpiry].Replace("\"", "")) - 20);
                    }
                    //put in raw list
                    responseCollection.ToList().ForEach(x => ConnectionToken.ResponseCollection.Add(x.Key, x.Value));
                    logger.Info("Access Token successfully received");
                    isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.AccessTokenResponseInvalid(responseCollection), ex);
                throw new OAuthException(ErrorMessages.AccessTokenResponseInvalid(responseCollection), ex);
            }
        }