public IToken GetToken(string baseUri, IToken consumer, string endPoint, string header) { var req = new HttpClient(baseUri) { UserAgent = "Xero Api wrapper - " + consumer.ConsumerKey }; EventHandler <ApiCallEventArgs> apiCalledHandler = (sender, e) => ApiCalled?.Invoke(this, e); req.ApiCalled += apiCalledHandler; req.AddHeader("Authorization", header); var response = req.Post(endPoint, string.Empty); req.ApiCalled -= apiCalledHandler; if (response.StatusCode != HttpStatusCode.OK) { if (response.Body.Contains("oauth_problem")) { throw new OAuthException(response.Body); } throw new UnexpectedOauthResponseException(response.StatusCode, response.Body); } var qs = HttpUtility.ParseQueryString(response.Body); var expires = qs["oauth_expires_in"]; var session = qs["oauth_session_handle"]; var token = new Token(consumer.ConsumerKey, consumer.ConsumerSecret) { TokenKey = qs["oauth_token"], TokenSecret = qs["oauth_token_secret"], OrganisationId = qs["xero_org_muid"] }; if (!string.IsNullOrWhiteSpace(expires)) { token.ExpiresAt = DateTime.UtcNow.AddSeconds(int.Parse(expires)); } if (!string.IsNullOrWhiteSpace(session)) { token.Session = session; token.SessionExpiresAt = DateTime.UtcNow.AddSeconds(int.Parse(qs["oauth_authorization_expires_in"])); } return(token); }
public IToken GetToken(string baseUri, IToken consumer, string endPoint, string header) { var req = new HttpClient(baseUri) { UserAgent = "Xero Api wrapper - " + consumer.ConsumerKey }; if (_clientCertificate != null) { req.ClientCertificate = _clientCertificate; } req.AddHeader("Authorization", header); var response = req.Post(endPoint, string.Empty); if (response.StatusCode != HttpStatusCode.OK) { throw new OAuthException(response.Body); } var qs = HttpUtility.ParseQueryString(response.Body); var expires = qs["oauth_expires_in"]; var session = qs["oauth_session_handle"]; var token = new Token(consumer.ConsumerKey, consumer.ConsumerSecret) { TokenKey = qs["oauth_token"], TokenSecret = qs["oauth_token_secret"], OrganisationId = qs["xero_org_muid"] }; if (!string.IsNullOrWhiteSpace(expires)) { token.ExpiresAt = DateTime.UtcNow.AddSeconds(int.Parse(expires)); } if (!string.IsNullOrWhiteSpace(session)) { token.Session = session; token.SessionExpiresAt = DateTime.UtcNow.AddSeconds(int.Parse(qs["oauth_authorization_expires_in"])); } return(token); }