Sky Api Authorization using RestSharp

Hi, I am a new user of the Sky Api and I am having trouble authenticating from a desktop app (WinForms, C#) using RestSharp. My problem is that I don't understand how to retrieve the access token from the redirect uri. Does anyone have any sample code they would be willing to share?


Thanks,

John

Comments

  • Hi John,


    Here's some C# code I use to get the access token from an incoming HTTPRequestMessage - I'm not using RestSharp so I can't comment on how that part of your code should work:

    // In the code below, request is incoming the HTTPRequestMessage
    Dictionary<string, string> query = request.GetQueryNameValuePairs().ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

    // check for an error
    if (query.ContainsKey("error"))
    {
    throw new Exception(query["error_message"]);
    }
    string authCode = query["code"];

    The code here is retrieved from the request's URL, which should have the token as one of the GET parameters (if successful - if not, you'll get the "error" and "error_message" parameters instead).


    Hope that helps!

  • Mathew,


    Thanks, I really appreciate the response, however, I don't think I explained my problem very well... I think I know how to parse the response when I get it, but I am not getting that information back from my POST.  Maybe I am trying to make it too complicated, but when I read the DOC it sounded like the callback response with the authorization token is sent to the callback uri (and not a reply back to the POST).  Did you create a separate httpListener to handle the callback? That is the part that has me baffled:)


    John
  • Hi John, sorry - yeah, I didn't quite get which part of the stage you were at. I can go through the full token retrieval flow I have in my app, if it helps:
    1. I send the user to the Blackbaud auth URL with my app ID and my redirect URI (https://oauth2.sky.blackbaud.com/authorization?client_id=xxx&response_type=code&redirect_uri=xxx&state=xxx
    2. The user completes the authorization
    3. Blackbaud makes a call to my callback URL with my authorization code
    4. I use that authorization code to create a POST call to retrieve the user's token
    I'll also share the rest of the code (a bit truncated) I'm using to get the auth code from the callback request and then use it to get a token. I'm not using RestSharp - I'm using a library called Flurl instead, but maybe this will help - might give you the gist of things, but let me know if you need clarification:

                Dictionary<string, string> query = request.GetQueryNameValuePairs().ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
                string authCode = query["code"];
                RENXTTokenResponse response = "https://oauth2.sky.blackbaud.com/token"
                    .WithBasicAuth(Integration.ApplicationId, Integration.ApplicationSecret)
                    .PostUrlEncodedAsync(new
                    {
                        grant_type = "authorization_code",
                        code = authCode,
                        redirect_uri = <my redirect URI>
                    })
                    .ReceiveJson<RENXTTokenResponse>()
                    .GetAwaiter()
                    .GetResult();

                DateTime now = DateTime.UtcNow;
                return new Authorization()
                {
                    AccessToken = response.access_token,
                    AccessTokenExpiry = now.AddSeconds(response.expires_in),
                    RefreshToken = response.refresh_token,
                    RefreshTokenExpiry = now.AddSeconds(response.refresh_token_expires_in),
                    IntegrationUserId = response.user_id
                };
  • Matthew,


    Thanks again - I think that I have it mostly working now.  I appreciate your help.


    John

Categories