Got a 'How do I' question? Join 'Ask a Trainer' Monday to Friday, 11am to 4pm ET for instant help and pro tips!

Beginner C# V3 API Example

SOLVED
Go to solution
SWCole
Rookie

I'm attempting to use the API for a C# ASP.net project.  Unfortunately most of the examples seem to be around Version 2 of the API, or in PHP.  I'm mostly just looking to get a list of contacts.  Is there example code for C# that might be around, that shows the authentication, refresh of key and downloading a contact list?

 

1 ACCEPTED SOLUTION
David_B.
Employee
0 Votes

Hello,

Thank you for reaching out to Constant Contact API Developer Support.

Here is an example of what a call to obtain an access token might look like:

string authorization_code = "{authorization code}"
var client = new RestClient("https://idfed.constantcontact.com/as/token.oauth2?code="+ authorization_code +"&redirect_uri={redirect URI}&grant_type=authorization_code&client_secret={secret}");
var request = new RestRequest(Method.POST);
 
string credentials = "{api key}:{secret}"
byte[] plain = System.Text.Encoding.UTF8.GetBytes(credentials);
base64cred = Convert.ToBase64String(plain);
var base64auth = "Basic " + base64cred;
request.AddHeader("authorization", base64auth);
 
IRestResponse response = client.Execute(request);


Note that this is what your app would do once the user is redirected to your app after allowing access during the OAuth process. You can use the RestRequest method to make other calls, such as POST calls to refresh the token.

Please have a look and let me know if you have any questions!

Regards,
David B.
Tier II API Support Engineer

View solution in original post

11 REPLIES 11
David_B.
Employee
0 Votes

Hello,

Thank you for reaching out to Constant Contact API Developer Support.

Here is an example of what a call to obtain an access token might look like:

string authorization_code = "{authorization code}"
var client = new RestClient("https://idfed.constantcontact.com/as/token.oauth2?code="+ authorization_code +"&redirect_uri={redirect URI}&grant_type=authorization_code&client_secret={secret}");
var request = new RestRequest(Method.POST);
 
string credentials = "{api key}:{secret}"
byte[] plain = System.Text.Encoding.UTF8.GetBytes(credentials);
base64cred = Convert.ToBase64String(plain);
var base64auth = "Basic " + base64cred;
request.AddHeader("authorization", base64auth);
 
IRestResponse response = client.Execute(request);


Note that this is what your app would do once the user is redirected to your app after allowing access during the OAuth process. You can use the RestRequest method to make other calls, such as POST calls to refresh the token.

Please have a look and let me know if you have any questions!

Regards,
David B.
Tier II API Support Engineer

SteveC082
Rookie

I have a similar need, but in an automated (as in unattended) system scenario. How do I get past the Oauth interaction?

David_B.
Employee
0 Votes

Hello,

 

Our V3 API is only able to be used with OAuth so there is no way to bypass the initial connection. However, once you have connected a Constant Contact account and received the initial access token and refresh token, you would use the refresh flow to exchange the refresh token for a new access token and refresh token without having to have the account owner log in again.

 

Please let me know if you have any other questions.

 

Sincerely,
David B.
Tier II API Support Engineer

SteveP245
Marketing Legend

Thanks for the example but RestClient is not a native .net object.  Could you please give a COMPLETE .net example of doing this from a non-user facing system using just native .net objects?  That is, probably like many companies, we have 0 need for anything client side; we just want to manage our lists from little internal .net apps.  Callbacks to URLs are confusing for non-web apps.  Can we just have an example of a .net console app that authenticates then calls an API or two?  Ideally the ones to create a list (which looks easy) and the one to bulk load from a .csv file (which looks a bit more confusing).  Again, please no magic handwaving about getting the authentication code or "put redirect URL here" since those make no sense in a non-web app.

ARRT
Campaign Collaborator
0 Votes

Same situation as you. Apparently they are unaware there are other OAuth flows that allow for server to server communication without redirect_uri's... We will continue to use the V1 API we are using now as it provides this capability. So sad they have chosen this route with their API design.

JulieH316
Rookie
0 Votes

Where can I find a v1 API example and docs or could you share yours? I need automated solution without oAuth redirect as well...

SteveP245
Marketing Legend
0 Votes

V1 API?  No idea but try looking at the code I added here some time ago for the v3 api.  It's fragments of code from the app I wrote so some of it won't make any sense in your app but you'll get the idea for how to deal with auth and refresh tokens to do what you need to get done.  Good luck!

 

https://community.constantcontact.com/t5/Getting-Started-with-API-s/V3-Starter-NET-Project/m-p/34029...

 

Thanks, Steve

YuryE7
Rookie
0 Votes

I want to make call to yoru api v3 from php. Do you have some examples to create contacts and lists please?

stanm
Rookie
0 Votes

I am having trouble understanding CC's terms.  api key is the key returned when you create an api application, right?

What is the authorization code?  Is that your CC login name or password?  If not what is it?

Where does the secret associated with the api key come from?

Since I am running from C# do I need a redirect uri?  c# is not running necessarily running the the web?

 

 

MetroMattress
Campaign Contributor

I am in the same boat as you on V3 examples. Have you been able to get a project working with C# that you'd be willing to share perhaps?

steveb3156
Rookie
0 Votes

Hello,

 

The new CTCT SDK for API v.3 template is available now here: https://github.com/mikeks/constantcontact-v3

 

Currently the SDK can be used to create (POST), update (PUT) and schedule custom code emails. It also has a method to renew Access Token. The library is used for in-home email automation scenario. I made the SDK public available. Please feel free to use it as the initial template. Please feel free to collaborate to add more features to the library.

 

Best regards,

Mike Keskinov

Resources
Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Announcements

API Updates

Join our list to be notified of new features and updates to our V3 API.

Sign Up