public async Task <string> GetToken(TokenCreate model) { var pairs = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", model.username), new KeyValuePair <string, string> ("Password", model.password) }; var content = new FormUrlEncodedContent(pairs); HttpResponseMessage response = _httpClient.PostAsync($"{Url}token", content).Result; var result = response.Content.ReadAsStringAsync().Result; Dictionary <string, string> tokenDictionary = JsonConvert.DeserializeObject <Dictionary <string, string> >(result); string token = tokenDictionary["access_token"]; _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); if (response.IsSuccessStatusCode) { var tokenObject = await response.Content.ReadAsAsync <Token>(); return(token); } return(null); }
/// <summary> /// Create Creates the entity with the given properties. /// </summary> /// <exception cref="Wallee.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="spaceId"></param> /// <param name="entity">The token object with the properties which should be created.</param> /// <returns>ApiResponse of Token</returns> public ApiResponse <Token> CreateWithHttpInfo(long?spaceId, TokenCreate entity) { // verify the required parameter 'spaceId' is set if (spaceId == null) { throw new ApiException(400, "Missing required parameter 'spaceId' when calling TokenService->Create"); } // verify the required parameter 'entity' is set if (entity == null) { throw new ApiException(400, "Missing required parameter 'entity' when calling TokenService->Create"); } var localVarPath = "/token/create"; var localVarPathParams = new Dictionary <String, String>(); var localVarQueryParams = new List <KeyValuePair <String, String> >(); var localVarHeaderParams = new Dictionary <String, String>(this.Configuration.DefaultHeader); var localVarFormParams = new Dictionary <String, String>(); var localVarFileParams = new Dictionary <String, FileParameter>(); Object localVarPostBody = null; // to determine the Content-Type header String[] localVarHttpContentTypes = new String[] { "application/json;charset=utf-8" }; String localVarHttpContentType = this.Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes); // to determine the Accept header String[] localVarHttpHeaderAccepts = new String[] { "application/json;charset=utf-8" }; String localVarHttpHeaderAccept = this.Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts); if (localVarHttpHeaderAccept != null) { localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept); } if (spaceId != null) { localVarQueryParams.AddRange(this.Configuration.ApiClient.ParameterToKeyValuePairs("", "spaceId", spaceId)); // query parameter } if (entity != null && entity.GetType() != typeof(byte[])) { localVarPostBody = this.Configuration.ApiClient.Serialize(entity); // http body (model) parameter } else { localVarPostBody = entity; // byte array } this.Configuration.ApiClient.ResetTimeout(); // make the HTTP request IRestResponse localVarResponse = (IRestResponse)this.Configuration.ApiClient.CallApi(localVarPath, Method.POST, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams, localVarPathParams, localVarHttpContentType); int localVarStatusCode = (int)localVarResponse.StatusCode; if (ExceptionFactory != null) { Exception exception = ExceptionFactory("Create", localVarResponse); if (exception != null) { throw exception; } } return(new ApiResponse <Token>(localVarStatusCode, localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), (Token)this.Configuration.ApiClient.Deserialize(localVarResponse, typeof(Token)))); }
/// <summary> /// Create Creates the entity with the given properties. /// </summary> /// <exception cref="Wallee.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="spaceId"></param> /// <param name="entity">The token object with the properties which should be created.</param> /// <returns>Token</returns> public Token Create(long?spaceId, TokenCreate entity) { ApiResponse <Token> localVarResponse = CreateWithHttpInfo(spaceId, entity); return(localVarResponse.Data); }