Obtaining Authentication using Blackbaud Sky API with C# Web Application

I feel a bit dumb asking this question, but I've gone through as many of the reference resources available on the site, and I'm having trouble figuring out how to request a subscription key from Blackbaud OAuth 2.0 Service from my application?  When I use the web based API ("Try it") I can select Authorization code and the subscription key field is then populated with a code that I'm able to paste into my application and use for an hour.  But I know there must be a way to request that using the SKY API. 


I'm just trying to build a simple donation website for our organization.  Again the application works fine when I obtain the subscription key manually, and I get the data response that I'm looking for, but I just don't know how to request it dynamically in my code.  Could someone please point me in the right direction?  I've lost so much time on my project trying to figure this out, and it's time to humble my self and request the support of others. :)  


Thank you so much for your help.  If you have any questions that require clarification, please let me know and I'll be glad to provide it.

Rob

Comments

  • Hi Robert,


    You may have already checked out the SKY authorization documentation, but if not, that is the place to start.


    Your subscription keys are available from Developer account/My subscriptions. (I assume you have access to https://developer.blackbaud.com/subscriptions.)


    But what I think you're really after is the authorization code flow which will result in you having a renewable token set (Refresh and Access tokens) that you can use to make API calls.


    This is where I think you might need to (re)visit the documentation.


    You can also grab a copy of our SKYLib•NET API Library and SDK. SKYLib makes the whole development process much easier and faster - you can literally have an app up and running in a few minutes. And it also includes a couple of ready-made demonstration applications - one in VB.NET, the other in C#.NET - which you can use as a base to kick off your own app. The demonstration apps can also serve to simply obtain/renew your authorization tokens. And you can use the whole thing free-of-charge in call-rate limited (1 call/s) mode which might be all you need. (A subscription unlocks the full speed call rate of up to 10 call/s.)


    I hope that helps.


    Cheers,

    Steve Cinquegrana | CEO and Principal Developer | Protégé Solutions

     
  • Figured it out... Here's a working example for the next person trying to figure this out.

    var url = "https://oauth2.sky.blackbaud.com/token";

    var httpRequest = (HttpWebRequest)WebRequest.Create(url);
    httpRequest.Method = "POST";

    httpRequest.Headers["Authorization"] = "Basic <Your Code>";
    httpRequest.ContentType = "application /x-www-form-urlencoded";

    var data = "grant_type=authorization_code&redirect_uri=" + redirect_uri + "&code=" + retCode;

    using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
    {
    streamWriter.Write(data);
    }

    try
    {
    var httpResponse = (HttpWebResponse)httpRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
    var result = streamReader.ReadToEnd();
    token = JsonConvert.DeserializeObject<CToken>(result);
    }
    }
    catch (Exception ex)
    {
    ViewBag.Error = ex.Message;
    }
    }