Back to Collection Items
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
-- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
DECLARE @req int
-- Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
EXEC sp_OAMethod @req, 'AddParam', NULL, 'scope', 'write'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'response_type', 'code'
DECLARE @jsonParam3 int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonParam3 OUT
EXEC sp_OAMethod @jsonParam3, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_id', @sTmp0
DECLARE @jsonParam4 int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonParam4 OUT
EXEC sp_OAMethod @jsonParam4, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @req, 'AddParam', NULL, 'redirect_uri', @sTmp0
EXEC sp_OAMethod @req, 'AddParam', NULL, 'decision', 'allow'
DECLARE @jsonParam6 int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonParam6 OUT
EXEC sp_OAMethod @jsonParam6, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @req, 'AddParam', NULL, 'csrf', @sTmp0
EXEC sp_OAMethod @req, 'AddParam', NULL, 'state', 'abc123'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'service', 'PasswordGrant'
EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Authorization', 'Bearer <access_token>'
EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Cookie', '{{cookieName}}={{demoSSOToken}}'
DECLARE @resp int
EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/authorize', @req
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @jsonParam3
EXEC @hr = sp_OADestroy @jsonParam4
EXEC @hr = sp_OADestroy @jsonParam6
RETURN
END
DECLARE @sbResponseBody int
-- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
DECLARE @jResp int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @jsonParam3
EXEC @hr = sp_OADestroy @jsonParam4
EXEC @hr = sp_OADestroy @jsonParam6
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
EXEC @hr = sp_OADestroy @resp
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "args": {
-- "client_id": "forgerockDemoConfidentialClient",
-- "code": "oIQFFQ1S5Fxnqs5U_EMWxYEJktQ",
-- "iss": "http://openam.example.com:8080/openam/oauth2"
-- },
-- "data": "",
-- "files": {},
-- "form": {},
-- "headers": {
-- "Accept": "*/*",
-- "Accept-Encoding": "gzip, deflate, br",
-- "Cookie": "amlbcookie=01; iPlanetDirectoryPro=sOpI1RvbCgvlQk.*AAJTSQACMDEA.*",
-- "Host": "httpbin.org",
-- "Referer": "http://openam.example.com:8080/openam/oauth2/realms/root/authorize",
-- "User-Agent": "PostmanRuntime/7.26.3",
-- "X-Amzn-Trace-Id": "Root=1-5f352dc7-0d6bd5fceafcd01e856febd2"
-- },
-- "json": null,
-- "method": "GET",
-- "origin": "5.65.200.229",
-- "url": "https://httpbin.org/anything?code=oIQFFQ1S5Fxnqs5U_EMWxYEJktQ&iss=http:%2F%2Fopenam.example.com:8080%2Fopenam%2Foauth2&client_id=forgerockDemoConfidentialClient"
-- }
-- Sample code for parsing the JSON response...
-- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
DECLARE @Client_id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Client_id OUT, 'args.client_id'
DECLARE @Code nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Code OUT, 'args.code'
DECLARE @Iss nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Iss OUT, 'args.iss'
DECLARE @data nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @data OUT, 'data'
DECLARE @Accept nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Accept OUT, 'headers.Accept'
DECLARE @Accept_Encoding nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Accept_Encoding OUT, 'headers.Accept-Encoding'
DECLARE @Cookie nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Cookie OUT, 'headers.Cookie'
DECLARE @Host nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Host OUT, 'headers.Host'
DECLARE @Referer nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Referer OUT, 'headers.Referer'
DECLARE @User_Agent nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @User_Agent OUT, 'headers.User-Agent'
DECLARE @X_Amzn_Trace_Id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @X_Amzn_Trace_Id OUT, 'headers.X-Amzn-Trace-Id'
DECLARE @json nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @json OUT, 'json'
DECLARE @method nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @method OUT, 'method'
DECLARE @origin nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @origin OUT, 'origin'
DECLARE @url nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @url OUT, 'url'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @jsonParam3
EXEC @hr = sp_OADestroy @jsonParam4
EXEC @hr = sp_OADestroy @jsonParam6
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
-H "Cookie: {{cookieName}}={{demoSSOToken}}"
--data-urlencode 'scope=write'
--data-urlencode 'response_type=code'
--data-urlencode 'client_id={{postmanConfidentialClientId}}'
--data-urlencode 'redirect_uri={{redirect_uri}}'
--data-urlencode 'decision=allow'
--data-urlencode 'csrf={{demoSSOToken}}'
--data-urlencode 'state=abc123'
--data-urlencode 'service=PasswordGrant'
https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/authorize
Postman Collection Item JSON
{
"name": "Step 2: Get Authorization Code",
"event": [
{
"listen": "test",
"script": {
"exec": [
"const jsonData = pm.response.json();",
"",
"if(pm.response.code == 200)",
"{",
" if(jsonData.args.code && jsonData.args.code != \"\") {",
" pm.globals.set(\"authorization_code\", jsonData.args.code);",
" }",
"}",
"",
"// TESTS",
"",
"pm.test(\"Follow redirects is enabled in Postman (Status code not 302)\", () => {",
" // If response was 302, ensure Postman is following redirects. ",
" pm.response.to.not.have.status(302);",
"});",
"",
"pm.test(\"Response from httpbin contained `code` argument\", () => {",
" pm.expect(jsonData.args.code).to.be.a(\"string\");",
"});"
],
"type": "text/javascript"
}
}
],
"protocolProfileBehavior": {
"followRedirects": true
},
"request": {
"method": "POST",
"header": [
{
"key": "Cookie",
"value": "{{cookieName}}={{demoSSOToken}}",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "scope",
"value": "write ",
"description": "Strings that are presented to the user for approval and included in tokens so that the protected resource may make decisions about what to give access to.",
"type": "text"
},
{
"key": "response_type",
"value": "code",
"description": "Response types the client will support and use.",
"type": "text"
},
{
"key": "client_id",
"value": "{{postmanConfidentialClientId}}",
"description": "The ID of the Confidential OAuth Client.",
"type": "text"
},
{
"key": "redirect_uri",
"value": "{{redirect_uri}}",
"description": "The complete URI to which client redirects the user if the request is successful.",
"type": "text"
},
{
"key": "decision",
"value": "allow",
"description": "The complete URI to which client redirects the user if the request is successful.",
"type": "text"
},
{
"key": "csrf",
"value": "{{demoSSOToken}}",
"description": "SSO token of the demo user, to protect against cross-site request forgery.",
"type": "text"
},
{
"key": "state",
"value": "abc123",
"type": "text"
},
{
"key": "service",
"value": "PasswordGrant",
"type": "text"
}
]
},
"url": {
"raw": "{{amUrl}}/oauth2{{realm}}/authorize",
"host": [
"{{amUrl}}"
],
"path": [
"oauth2{{realm}}",
"authorize"
]
},
"description": "Get the authorization code by making a call to the authorization server's authorization endpoint, specifying the SSO token of the user.\n"
},
"response": [
{
"name": "Success - headers captured by HttpBin.",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "scope",
"value": "write ",
"description": "Strings that are presented to the user for approval and included in tokens so that the protected resource may make decisions about what to give access to.",
"type": "text"
},
{
"key": "response_type",
"value": "code",
"description": "Response types the client will support and use.",
"type": "text"
},
{
"key": "client_id",
"value": "{{postmanConfidentialClientId}}",
"description": "The ID of the Confidential OAuth Client.",
"type": "text"
},
{
"key": "redirect_uri",
"value": "{{redirect_uri}}",
"description": "The complete URI to which client redirects the user if the request is successful.",
"type": "text"
},
{
"key": "decision",
"value": "allow",
"description": "The complete URI to which client redirects the user if the request is successful.",
"type": "text"
},
{
"key": "csrf",
"value": "{{demoSSOToken}}",
"description": "SSO token of the demo user, to protect against cross-site request forgery.",
"type": "text"
}
]
},
"url": {
"raw": "{{amUrl}}/oauth2{{realm}}/authorize",
"host": [
"{{amUrl}}"
],
"path": [
"oauth2{{realm}}",
"authorize"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Thu, 13 Aug 2020 12:10:47 GMT"
},
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Content-Length",
"value": "937"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Server",
"value": "gunicorn/19.9.0"
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Credentials",
"value": "true"
}
],
"cookie": [
],
"body": "{\n \"args\": {\n \"client_id\": \"forgerockDemoConfidentialClient\",\n \"code\": \"oIQFFQ1S5Fxnqs5U_EMWxYEJktQ\",\n \"iss\": \"http://openam.example.com:8080/openam/oauth2\"\n },\n \"data\": \"\",\n \"files\": {},\n \"form\": {},\n \"headers\": {\n \"Accept\": \"*/*\",\n \"Accept-Encoding\": \"gzip, deflate, br\",\n \"Cookie\": \"amlbcookie=01; iPlanetDirectoryPro=sOpI1RvbCgvlQk.*AAJTSQACMDEA.*\",\n \"Host\": \"httpbin.org\",\n \"Referer\": \"http://openam.example.com:8080/openam/oauth2/realms/root/authorize\",\n \"User-Agent\": \"PostmanRuntime/7.26.3\",\n \"X-Amzn-Trace-Id\": \"Root=1-5f352dc7-0d6bd5fceafcd01e856febd2\"\n },\n \"json\": null,\n \"method\": \"GET\",\n \"origin\": \"5.65.200.229\",\n \"url\": \"https://httpbin.org/anything?code=oIQFFQ1S5Fxnqs5U_EMWxYEJktQ&iss=http:%2F%2Fopenam.example.com:8080%2Fopenam%2Foauth2&client_id=forgerockDemoConfidentialClient\"\n}"
}
]
}