For 2 days I have been looking how to connect and perform queries in a web api, but it still did not work.. I need help, someone here can give me some example how i can connect to my webapi from C# Winforms. See below for some information:
Server for authentication: http://ift.tt/2sJrufO grant_type="client_credentials"
clientId="property-search-web-site"
clientSecret="999999999"
tenant="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
post / x-www-form-urlencoded
Someone have example or can help fix my code:
public class Token
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }
[JsonProperty("token_type")]
public string TokenType { get; set; }
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
[JsonProperty("refresh_token")]
public string RefreshToken { get; set; }
[JsonProperty("error")]
public string Error { get; set; }
}
public static string GenerateApiKey()
{
var appSettings = ConfigurationManager.AppSettings;
try
{
string tenant = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
var urlToCall = string.Format("http://ift.tt/2sJrufO" + tenant + "/oauth/token");
var uri = new Uri(urlToCall);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
var postItems = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", "property-search-web-site"),
new KeyValuePair<string, string>("client_secret", "999999999"),
};
string postData = string.Join("&",
postItems.Select(kvp =>
string.Format("{0}={1}", kvp.Key, HttpUtility.UrlEncode(kvp.Value))));
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
ServicePointManager.Expect100Continue = false;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
ServicePointManager.Expect100Continue = false;
var response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
}
return "";
}
catch (Exception e)
{
throw new Exception("Error getting a response from API " + e.Message);
}
}
Aucun commentaire:
Enregistrer un commentaire