Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) OAuth2 Token using IdentityServer4 with Client CredentialsDemonstrates how to get an OAuth2 access token using the client credential flow with IdentityServer4.
#include <CkHttp.h> #include <CkHttpResponse.h> #include <CkJsonObject.h> #include <CkJsonArray.h> #include <CkHttpRequest.h> void ChilkatSample(void) { CkString strOut; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkHttp http; // The first step is to fetch your IdentityServer4's discovery document // (OpenID Connect defines a discovery mechanism, called OpenID Connect Discovery, where an OpenID server publishes its metadata at a well-known URL, // typically https://server.com/.well-known/openid-configuration CkHttpResponse *resp = http.QuickRequest("GET","https://localhost:5000/.well-known/openid-configuration"); if (http.get_LastMethodSuccess() != true) { strOut.append(http.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } if (resp->get_StatusCode() != 200) { strOut.append("Received response status code "); strOut.appendInt(resp->get_StatusCode()); strOut.append("\r\n"); strOut.append("Response body containing error text or JSON:"); strOut.append("\r\n"); strOut.append(resp->bodyStr()); strOut.append("\r\n"); delete resp; SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } CkJsonObject json; bool success = json.Load(resp->bodyStr()); delete resp; // We have the discovery document, which contains something like this: // You can use this online tool to generate parsing code from sample JSON: // Generate Parsing Code from JSON // { // "issuer": "https://localhost:5000", // "jwks_uri": "https://localhost:5000/.well-known/openid-configuration/jwks", // "authorization_endpoint": "https://localhost:5000/connect/authorize", // "token_endpoint": "https://localhost:5000/connect/token", // "userinfo_endpoint": "https://localhost:5000/connect/userinfo", // "end_session_endpoint": "https://localhost:5000/connect/endsession", // "check_session_iframe": "https://localhost:5000/connect/checksession", // "revocation_endpoint": "https://localhost:5000/connect/revocation", // "introspection_endpoint": "https://localhost:5000/connect/introspect", // "frontchannel_logout_supported": true, // "frontchannel_logout_session_supported": true, // "backchannel_logout_supported": true, // "backchannel_logout_session_supported": true, // "scopes_supported": [ // "openid", // "profile", // "email", // "MyCompany.profile", // "MyCompany.Identity.WebApi", // "MyCompany.TriHub.WebApi", // "offline_access" // ], // "claims_supported": [ // "sub", // "updated_at", // "locale", // "zoneinfo", // "birthdate", // "gender", // "website", // "profile", // "preferred_username", // "nickname", // "middle_name", // "given_name", // "family_name", // "name", // "picture", // "email_verified", // "email", // "userId", // "groups", // "fullname" // ], // "grant_types_supported": [ // "authorization_code", // "client_credentials", // "refresh_token", // "implicit", // "password" // ], // "response_types_supported": [ // "code", // "token", // "id_token", // "id_token token", // "code id_token", // "code token", // "code id_token token" // ], // "response_modes_supported": [ // "form_post", // "query", // "fragment" // ], // "token_endpoint_auth_methods_supported": [ // "client_secret_basic", // "client_secret_post" // ], // "subject_types_supported": [ // "public" // ], // "id_token_signing_alg_values_supported": [ // "RS256" // ], // "code_challenge_methods_supported": [ // "plain", // "S256" // ] // } // // The next steps are to (1) get the token_endpoint, // and (2) verify that the client_credentials grant type is supported. const char *tokenEndpoint = json.stringOf("token_endpoint"); CkJsonArray *grantTypes = json.ArrayOf("grant_types_supported"); int clientCredentialsIdx = grantTypes->FindString("client_credentials",true); delete grantTypes; // If clientCredentialsIdx is less then zero (-1) then the "client_credentials" string was not found. if (clientCredentialsIdx < 0) { strOut.append("The client credentials grant type is not supported."); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Request the access token using our Client ID and Client Secret. // We're going to duplicate this CURL statement: // curl --request POST \ // --url '<tokenEndpoint>' \ // --header 'content-type: application/x-www-form-urlencoded' \ // --data 'grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET' CkHttpRequest req; req.put_HttpVerb("POST"); req.AddParam("grant_type","client_credentials"); req.AddParam("client_id","CLIENT_ID"); req.AddParam("client_secret","CLIENT_SECRET"); resp = http.PostUrlEncoded(tokenEndpoint,req); if (http.get_LastMethodSuccess() == false) { strOut.append(http.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Make sure we got a 200 response status code, otherwise it's an error. if (resp->get_StatusCode() != 200) { strOut.append("POST to token endpoint failed."); strOut.append("\r\n"); strOut.append("Received response status code "); strOut.appendInt(resp->get_StatusCode()); strOut.append("\r\n"); strOut.append("Response body containing error text or JSON:"); strOut.append("\r\n"); strOut.append(resp->bodyStr()); strOut.append("\r\n"); delete resp; SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } success = json.Load(resp->bodyStr()); delete resp; // Our JSON response should contain this: // { // "access_token":"eyJz93a...k4laUWw", // "token_type":"Bearer", // "expires_in":86400 // } // Get the access token: const char *accessToken = json.stringOf("access_token"); // The access token is what gets added to "Authorization: Bearer <access_token>" // for the subsequent REST API calls.. SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.