/// <summary> /// Obtains a request token /// </summary> /// <returns></returns> public async Task <OAuthRequestToken> GetRequestTokenAsync() { var consumer = new Consumer(_oAuthConfig.ConsumerKey, _oAuthConfig.ConsumerSecret, callbackUrl: _oAuthConfig.CallbackUrl); using (var client = new OAuthHttpClient(consumer)) { // Get Request Token var result = await client.GetAsync(_oAuthConfig.RequestTokenUrl); if (!result.IsSuccessStatusCode) // TODO: Replace with meaningful error message { throw new Exception($"Unable to get request token: {result.ReasonPhrase}"); } // Read token parameters var rawRequestTokenParameters = await result.Content.ReadAsStringAsync(); var requestTokenParameters = GetParametersFromString(rawRequestTokenParameters); // Verify if callback is confirmed bool callbackConfirmed; if (!bool.TryParse(requestTokenParameters["oauth_callback_confirmed"], out callbackConfirmed) || !callbackConfirmed) { return(null); } return(new OAuthRequestToken { RequestToken = requestTokenParameters["oauth_token"], RequestTokenSecret = requestTokenParameters["oauth_token_secret"], RawParameters = requestTokenParameters }); } }
/// <summary> /// Exchanges request token for an access token /// </summary> /// <param name="requestToken"></param> /// <param name="requestTokenSecret"></param> /// <param name="verifier"></param> /// <returns></returns> public async Task <OAuthAccessToken> GetAccessTokenAsync(string requestToken, string requestTokenSecret, string verifier) { var consumer = new Consumer(_oAuthConfig.ConsumerKey, _oAuthConfig.ConsumerSecret, requestToken, requestTokenSecret, verifier); using (var client = new OAuthHttpClient(consumer)) { // Get Access Token var result = await client.PostAsync(_oAuthConfig.AccessTokenUrl, null); if (!result.IsSuccessStatusCode) // TODO: Replace with meaningful error message { throw new Exception($"Unable to get access token: {result.ReasonPhrase}"); } // Read token parameters var rawAccessTokenParameters = await result.Content.ReadAsStringAsync(); var accessTokenParameters = GetParametersFromString(rawAccessTokenParameters); return(new OAuthAccessToken { AccessToken = accessTokenParameters["oauth_token"], AccessTokenSecret = accessTokenParameters["oauth_token_secret"], RawParameters = accessTokenParameters }); } }
public static async Task <JsonHttpResponseMessage <T> > SendAsync <T>( this OAuthHttpClient httpClient, HttpRequestMessage requestMessage) { var response = await httpClient.SendAsync(requestMessage); return(await response.Deserialize <T>()); }
/// <summary> /// Initializes a new instance of the <see cref="RestApiDemo" /> class. /// </summary> public RestApiDemo(string clientId, string clientSecret) { this.oauthSettings = new OAuth2Settings { ClientId = clientId, ClientSecret = clientSecret, AccessTokenUrl = "https://apps.xynaps.net/api/v1/oauth2/token", BaseUrl = "https://apps.xynaps.net/" }; this.client = new OAuthHttpClient(this.oauthSettings); }
} // Constructor needed for dynamic loading to find it protected MicrosoftGraphBackend(string url, string protocolKey, Dictionary <string, string> options) { string authid; options.TryGetValue(AUTHID_OPTION, out authid); if (string.IsNullOrEmpty(authid)) { throw new UserInformationException(Strings.MicrosoftGraph.MissingAuthId(OAuthHelper.OAUTH_LOGIN_URL(protocolKey)), "MicrosoftGraphBackendMissingAuthId"); } string fragmentSizeStr; if (options.TryGetValue(UPLOAD_SESSION_FRAGMENT_SIZE_OPTION, out fragmentSizeStr) && int.TryParse(fragmentSizeStr, out this.fragmentSize)) { // Make sure the fragment size is a multiple of the desired multiple size. // If it isn't, we round down to the nearest multiple below it. this.fragmentSize = (this.fragmentSize / UPLOAD_SESSION_FRAGMENT_MULTIPLE_SIZE) * UPLOAD_SESSION_FRAGMENT_MULTIPLE_SIZE; // Make sure the fragment size isn't larger than the maximum, or smaller than the minimum this.fragmentSize = Math.Max(Math.Min(this.fragmentSize, UPLOAD_SESSION_FRAGMENT_MAX_SIZE), UPLOAD_SESSION_FRAGMENT_MULTIPLE_SIZE); } else { this.fragmentSize = UPLOAD_SESSION_FRAGMENT_DEFAULT_SIZE; } string fragmentRetryCountStr; if (!(options.TryGetValue(UPLOAD_SESSION_FRAGMENT_RETRY_COUNT_OPTION, out fragmentRetryCountStr) && int.TryParse(fragmentRetryCountStr, out this.fragmentRetryCount))) { this.fragmentRetryCount = UPLOAD_SESSION_FRAGMENT_DEFAULT_RETRY_COUNT; } string fragmentRetryDelayStr; if (!(options.TryGetValue(UPLOAD_SESSION_FRAGMENT_RETRY_DELAY_OPTION, out fragmentRetryDelayStr) && int.TryParse(fragmentRetryDelayStr, out this.fragmentRetryDelay))) { this.fragmentRetryDelay = UPLOAD_SESSION_FRAGMENT_DEFAULT_RETRY_DELAY; } this.m_client = new OAuthHttpClient(authid, protocolKey); this.m_client.BaseAddress = new System.Uri(BASE_ADDRESS); // Extract out the path to the backup root folder from the given URI. Since this can be an expensive operation, // we will cache the value using a lazy initializer. this.rootPathFromURL = new Lazy <string>(() => MicrosoftGraphBackend.NormalizeSlashes(this.GetRootPathFromUrl(url))); }
public async Task<FinishedAuthorizedModel> FinishOAuth(HttpRequestBase request, string[] scopes) { //Finish processing oauth var auth = oauthClient.ProcessUserAuthorization(request); var url = GetTokenInfoUrl(auth.AccessToken, scopes); using (var client = new OAuthHttpClient(BaseUrl, "oauth", auth.AccessToken)) { //Validate token is correct var result = await client.GetResource<TokenInfoModel>(url); var tokenInfo = ValidateToken(result, ConsumerKey); return new FinishedAuthorizedModel { Audience = tokenInfo.Audience, User = tokenInfo.User, Scope = auth.Scope, AccessToken = auth.AccessToken, AccessTokenExpirationUtc = auth.AccessTokenExpirationUtc, RefreshToken = auth.RefreshToken }; } }
public EventsApi(string baseUrl, string accessToken) { client = new OAuthHttpClient(baseUrl, ApiBase, accessToken); }
public ExperienceTypesApi(string baseUrl, string accessToken) { client = new OAuthHttpClient(baseUrl, ApiBase, accessToken); }
public QuestionResponsesApi(string baseUrl, string accessToken) { client = new OAuthHttpClient(baseUrl, ApiBase, accessToken); }
public WorkflowProgressApi(string baseUrl, string accessToken) { client = new OAuthHttpClient(baseUrl, ApiBase, accessToken); }
public EduarteClient(ApplicationUrl applicationUrl, OAuthClient authClient, TokenResponse token) { _oAuthHttpClient = new OAuthHttpClient(authClient, token); _oAuthHttpClient.HttpClient.BaseAddress = applicationUrl.RestUri.Normalize(); _oAuthHttpClient.HttpClient.DefaultRequestHeaders.Add("Accept", "application/json"); }
public AppointmentBookingsApi(string baseUrl, string accessToken) { client = new OAuthHttpClient(baseUrl, ApiBase, accessToken); }