Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string encodedUserCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("user:password"));
- string userData = "username=" + Username + "&password=" + Password + "&grant_type=password";
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(BaseUrl + "/token");
- request.Accept = "application/json";
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.Headers.Add("Authorization", "Basic " + encodedUserCredentials);
- StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
- requestWriter.Write(userData);
- requestWriter.Close();
- var response = request.GetResponse() as HttpWebResponse;
- using (Stream stream = response.GetResponseStream())
- {
- StreamReader reader = new StreamReader(stream, Encoding.UTF8);
- String json = reader.ReadToEnd();
- ResponseObj deserializedJson = JsonConvert.DeserializeObject<ResponseObj>(json);
- UserToken Token = new UserToken
- {
- AccessToken = deserializedJson.access_token,
- TokenType = deserializedJson.token_type,
- ExpiresIn = deserializedJson.expires_in,
- };
- return Token;
- }
Add Comment
Please, Sign In to add comment