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
Thanks,
John
0
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!0 -
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:)
John0 -
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:
- 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
- The user completes the authorization
- Blackbaud makes a call to my callback URL with my authorization code
- I use that authorization code to create a POST call to retrieve the user's token
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
};1 -
Matthew,
Thanks again - I think that I have it mostly working now. I appreciate your help.
John0
Categories
- All Categories
- 6 Blackbaud Community Help
- 213 bbcon®
- 1.4K Blackbaud Altru®
- 400 Blackbaud Award Management™ and Blackbaud Stewardship Management™
- 1.1K Blackbaud CRM™ and Blackbaud Internet Solutions™
- 15 donorCentrics®
- 360 Blackbaud eTapestry®
- 2.6K Blackbaud Financial Edge NXT®
- 655 Blackbaud Grantmaking™
- 576 Blackbaud Education Management Solutions for Higher Education
- 3.2K Blackbaud Education Management Solutions for K-12 Schools
- 939 Blackbaud Luminate Online® and Blackbaud TeamRaiser®
- 84 JustGiving® from Blackbaud®
- 6.6K Blackbaud Raiser's Edge NXT®
- 3.7K SKY Developer
- 248 ResearchPoint™
- 119 Blackbaud Tuition Management™
- 165 Organizational Best Practices
- 241 Member Lounge (Just for Fun)
- 34 Blackbaud Community Challenges
- 34 PowerUp Challenges
- 3 (Open) PowerUp Challenge: Chat for Blackbaud AI
- 3 (Closed) PowerUp Challenge: Data Health
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Product Update Briefing
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Standard Reports+
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Email Marketing
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Gift Management
- 4 (Closed) Raiser's Edge NXT PowerUp Challenge: Event Management
- 3 (Closed) Raiser's Edge NXT PowerUp Challenge: Home Page
- 4 (Closed) Raiser's Edge NXT PowerUp Challenge: Standard Reports
- 4 (Closed) Raiser's Edge NXT PowerUp Challenge: Query
- 790 Community News
- 2.9K Jobs Board
- 53 Blackbaud SKY® Reporting Announcements
- 47 Blackbaud CRM Higher Ed Product Advisory Group (HE PAG)
- 19 Blackbaud CRM Product Advisory Group (BBCRM PAG)
