Back to Collection Items
integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_Req
oleobject loo_JsonParam2
oleobject loo_JsonParam3
oleobject loo_JsonParam4
oleobject loo_JsonParam5
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_Access_token
string ls_Instance_url
string ls_Id
string ls_Token_type
string ls_Issued_at
string ls_Signature
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_Http = create oleobject
// Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Req = create oleobject
// Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")
loo_Req.AddParam("grant_type","password")
loo_JsonParam2 = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JsonParam2.ConnectToNewObject("Chilkat.JsonObject")
loo_Req.AddParam("client_id",loo_JsonParam2.Emit())
loo_JsonParam3 = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JsonParam3.ConnectToNewObject("Chilkat.JsonObject")
loo_Req.AddParam("client_secret",loo_JsonParam3.Emit())
loo_JsonParam4 = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JsonParam4.ConnectToNewObject("Chilkat.JsonObject")
loo_Req.AddParam("username",loo_JsonParam4.Emit())
loo_JsonParam5 = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JsonParam5.ConnectToNewObject("Chilkat.JsonObject")
loo_Req.AddParam("password",loo_JsonParam5.Emit())
loo_Req.AddHeader("Accept","application/json")
loo_Resp = loo_Http.PostUrlEncoded("https://login.salesforce.com{{site}}/services/oauth2/token",loo_Req)
if loo_Http.LastMethodSuccess = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_Req
destroy loo_JsonParam2
destroy loo_JsonParam3
destroy loo_JsonParam4
destroy loo_JsonParam5
return
end if
loo_SbResponseBody = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")
loo_Resp.GetBodySb(loo_SbResponseBody)
loo_JResp = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")
loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0
Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()
li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
Write-Debug "Response Header:"
Write-Debug loo_Resp.Header
Write-Debug "Failed."
destroy loo_Resp
destroy loo_Http
destroy loo_Req
destroy loo_JsonParam2
destroy loo_JsonParam3
destroy loo_JsonParam4
destroy loo_JsonParam5
destroy loo_SbResponseBody
destroy loo_JResp
return
end if
destroy loo_Resp
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "access_token": "ACCESS_TOKEN_GOES_HERE",
// "instance_url": "https://pozil-dev-ed.my.salesforce.com",
// "id": "https://login.salesforce.com/id/00D58000000arpqEAA/00558000000yFyDAAU",
// "token_type": "Bearer",
// "issued_at": "1609942615640",
// "signature": "SIGNATURE_GOES_HERE"
// }
// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
ls_Access_token = loo_JResp.StringOf("access_token")
ls_Instance_url = loo_JResp.StringOf("instance_url")
ls_Id = loo_JResp.StringOf("id")
ls_Token_type = loo_JResp.StringOf("token_type")
ls_Issued_at = loo_JResp.StringOf("issued_at")
ls_Signature = loo_JResp.StringOf("signature")
destroy loo_Http
destroy loo_Req
destroy loo_JsonParam2
destroy loo_JsonParam3
destroy loo_JsonParam4
destroy loo_JsonParam5
destroy loo_SbResponseBody
destroy loo_JResp
Curl Command
curl -X POST
-H "Content-Type: application/x-www-form-urlencoded"
-H "Accept: application/json"
--data-urlencode 'grant_type=password'
--data-urlencode 'client_id={{clientId}}'
--data-urlencode 'client_secret={{clientSecret}}'
--data-urlencode 'username={{username}}'
--data-urlencode 'password={{password}}{{secretToken}}'
https://login.salesforce.com{{site}}/services/oauth2/token
Postman Collection Item JSON
{
"name": "Username Password Flow",
"event": [
{
"listen": "test",
"script": {
"exec": [
"const jsonData = pm.response.json();",
"const id = jsonData.id.split('/');",
"",
"const context = pm.environment.name ? pm.environment : pm.collectionVariables;",
"context.set(\"_accessToken\", jsonData.access_token);",
"context.set(\"_endpoint\", jsonData.instance_url);",
"context.set(\"_userId\", id.pop());",
"context.set(\"_orgId\", id.pop());",
""
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "noauth"
},
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
},
{
"key": "Accept",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "password",
"type": "text"
},
{
"key": "client_id",
"value": "{{clientId}}",
"type": "text"
},
{
"key": "client_secret",
"value": "{{clientSecret}}",
"type": "text"
},
{
"key": "username",
"value": "{{username}}",
"type": "text"
},
{
"key": "password",
"value": "{{password}}{{secretToken}}",
"type": "text"
}
]
},
"url": {
"raw": "{{url}}{{site}}/services/oauth2/token",
"host": [
"{{url}}{{site}}"
],
"path": [
"services",
"oauth2",
"token"
]
}
},
"response": [
{
"name": "Successful OAuth Username Password Login",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
},
{
"key": "Accept",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "password",
"type": "text"
},
{
"key": "client_id",
"value": "CLIENT_ID_GOES_HERE",
"type": "text"
},
{
"key": "client_secret",
"value": "CLIENT_SECRET_GOES_HERE",
"type": "text"
},
{
"key": "username",
"value": "demo@pozil.org",
"type": "text"
},
{
"key": "password",
"value": "PASSWORD_AND_SECURITY_TOKEN_GO_HERE",
"type": "text"
}
]
},
"url": {
"raw": "https://login.salesforce.com/services/oauth2/token",
"protocol": "https",
"host": [
"login",
"salesforce",
"com"
],
"path": [
"services",
"oauth2",
"token"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Wed, 06 Jan 2021 14:16:55 GMT"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=31536002; includeSubDomains"
},
{
"key": "Public-Key-Pins-Report-Only",
"value": "pin-sha256=\"9n0izTnSRF+W4W4WhQB8duS2bxVLfzXsY=\"; pin-sha256=\"5kJvNEMw0KjrCAu7eXY5HZdvSP91w=\"; pin-sha256=\"njN4rRG+zPUPHlv4+foULwl1g=\"; max-age=86400; includeSubDomains; report-uri=\"https://a.forcesslreports.com/hpkp-report/00D58000000arpqm\";"
},
{
"key": "Expect-CT",
"value": "max-age=86400, report-uri=\"https://a.forcesslreports.com/Expect-CT-report/00D58000000arpqm\""
},
{
"key": "X-Robots-Tag",
"value": "none"
},
{
"key": "X-B3-TraceId",
"value": "c21e47e40ecc875d"
},
{
"key": "X-B3-SpanId",
"value": "c21e47e40ecc875d"
},
{
"key": "X-B3-Sampled",
"value": "0"
},
{
"key": "Cache-Control",
"value": "no-cache,must-revalidate,max-age=0,no-store,private"
},
{
"key": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"key": "X-ReadOnlyMode",
"value": "false"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Vary",
"value": "Accept-Encoding"
},
{
"key": "Content-Encoding",
"value": "gzip"
},
{
"key": "Transfer-Encoding",
"value": "chunked"
}
],
"cookie": [
],
"body": "{\n \"access_token\": \"ACCESS_TOKEN_GOES_HERE\",\n \"instance_url\": \"https://pozil-dev-ed.my.salesforce.com\",\n \"id\": \"https://login.salesforce.com/id/00D58000000arpqEAA/00558000000yFyDAAU\",\n \"token_type\": \"Bearer\",\n \"issued_at\": \"1609942615640\",\n \"signature\": \"SIGNATURE_GOES_HERE\"\n}"
}
]
}