Sample C# Code With New APIKEY
private const string AllScopes = "general_info contacts finances events event_registrations account membership_levels settings";
var authData = string.Format("granttype={0}&scope={1}", "clientcredentials",AllScopes);
var response = System.Net.WebRequest.Create(Common.OAuthServiceUrl)
.SetBasicAuth("APIKEY", "........YOUR API KEY HERE......")
.SetData(authData)
.GetResponse();
Extension Methods:
public static WebRequest SetData(this WebRequest request, string data, string contentType = MimeFormUrlEncoded, string method = "POST")
{
request.Method = method;
request.ContentType = contentType;
byte[] sentData = Encoding.UTF8.GetBytes(data);
request.ContentLength = sentData.Length;
Stream sendStream = request.GetRequestStream();
sendStream.Write(sentData, 0, sentData.Length);
sendStream.Close();
return request;
}
public static WebRequest SetBasicAuth(this WebRequest request, string userName, string password)
{
var credentials = string.Format("{0}:{1}", userName, password);
var encoded = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(credentials));
request.Headers.Add("Authorization", "Basic " + encoded);
return request;
}
1
vote
Alan Churchill
shared this idea
This code can be used to work with API. There are also a github repository https://github.com/WildApricot/ApiSamples with several code samples for different languages.
-
Hi Alan,
Do you have a question or just share your code sample?