/// <summary> /// Requires that AccountId and ApplicationKey on the options object be set. If you are using an application key you must specify the accountId, the keyId, and the applicationKey. /// </summary> /// <param name="options"></param> /// <returns></returns> public static B2Options Authorize(B2Options options) { // Return if already authenticated. if (options.Authenticated) { return(options); } var client = HttpClientFactory.CreateHttpClient(options.RequestTimeout); var requestMessage = AuthRequestGenerator.Authorize(options); var response = client.SendAsync(requestMessage).Result; var jsonResponse = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode) { var authResponse = JsonConvert.DeserializeObject <B2AuthResponse>(jsonResponse); options.SetState(authResponse); } else if (response.StatusCode == HttpStatusCode.Unauthorized) { // Return a better exception because of confusing Keys api. throw new AuthorizationException("If you are using an Application key and not a Master key, make sure that you are supplying the Key ID and Key Value for that Application Key. Do not mix your Account ID with your Application Key."); } else { throw new AuthorizationException(jsonResponse); } return(options); }
/// <summary> /// Authorize against the B2 storage service. /// </summary> /// <returns>B2Options containing the download url, new api url, and authorization token.</returns> public async Task <B2Options> Authorize(CancellationToken cancelToken = default(CancellationToken)) { var client = HttpClientFactory.CreateHttpClient(); var requestMessage = AuthRequestGenerator.Authorize(_options); var response = await client.SendAsync(requestMessage, cancelToken); var jsonResponse = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var authResponse = JsonConvert.DeserializeObject <B2AuthResponse>(jsonResponse); _options.SetState(authResponse); return(_options); } else { throw new AuthorizationException(jsonResponse); } }
/// <summary> /// Requires that AccountId and ApplicationKey on the options object be set. /// </summary> /// <param name="options"></param> /// <returns></returns> public static B2Options Authorize(B2Options options) { var client = HttpClientFactory.CreateHttpClient(options.RequestTimeout); var requestMessage = AuthRequestGenerator.Authorize(options); var response = client.SendAsync(requestMessage).Result; var jsonResponse = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode) { var authResponse = JsonConvert.DeserializeObject <B2AuthResponse>(jsonResponse); options.SetState(authResponse); } else { throw new AuthorizationException(jsonResponse); } return(options); }