0

I'm putting together a process in Salesforce to work with PureCloud campaigns, and as it's the first OAuth process into PureCloud I've built I'm requesting some guidance. At this point I'm getting 'Bad Request' as the response, so (hopefully) it's something small and naive on my part that can be easily remedied. (I have also posted this on PureCloud's board; in general, there don't seem to be many other Salesforce developers that have worked with the Platform API over there.)

At the moment, I'm just putting together the request for an access token, and one piece that doesn't appear to be setting correctly is the grant_type. When I try to verify the header, I'm seeing null there.

Their example for getting the access token is:

POST /oauth/token HTTP/1.1
Host: login.mypurecloud.com
Content-Type: application/x-www-form-urlencoded
Authorization: Basic BASE64(<client_id>:<client_secret>)

grant_type=client_credentials

In Apex, the code is as follows:

HttpRequest req = new HttpRequest();

req.setMethod('POST');

req.setHeader('Host','login.mypurecloud.com/oauth');
req.setHeader('content-type', 'application/x-www-form-urlencoded');
req.setHeader('authorization', 'Basic BASE64(' + clientId + ':' + clientSecret + ')');
req.setHeader('grant_type', 'client_credentials');
req.setEndpoint('https://login.mypurecloud.com/oauth/token');

When I output the header, I get the following:

Method:POST
grant_type null
authorization Basic BASE64(--clientID--:--clientSecret--)
content-type application/x-www-form-urlencoded
Host login.mypurecloud.com/oauth
System.HttpRequest[Endpoint=https://login.mypurecloud.com/oauth/token, Method=POST]

So right now, it looks like the grant_type setting is invalid, at a minimum. Let me know if anything else jumps out at you, and thanks!

1

Basic authentication always uses a base64 encoded string in the form "username:password", so the example they gave is obviously confusing, but the Apex Code to do this would be as follows:

req.setHeader('Authorization', 'Basic ' +
  EncodingUtil.base64Encode(Blob.valueOf(clientId + ':' + clientSecret)));

The grant type goes in the body of the request, like this:

req.setBody('grant_type=client_credentials');
  • I did receive a comment about the grant type: "grant_type isn't a header, it's a form data property." I took that out, though I don't know whether it needs to be in there anyway, in a different form..(?) After a couple of tweaks to the URL, I've been upgraded to "invalid_request". (p.s. Thank you for the format updates :D ) – Duncan Stewart Apr 27 '18 at 17:52
  • @DuncanStewart Oh, yeah, I missed that. Let me edit... – sfdcfox Apr 27 '18 at 17:53
  • Thanks much -- now I'm actually able to see the real issue: "STATUS_CODE:{"error":"unauthorized_client","description":"client is not authorized to use the client_credential grant type"}" Time to reach out to PureCloud and see whether that can be ameliorated. – Duncan Stewart Apr 27 '18 at 17:56
  • @DuncanStewart Yes, it looks like your Apex Code is right now. Hopefully they can get you the rest of the way. – sfdcfox Apr 27 '18 at 17:57
0

After creating an OAuth client on the PureCloud side with the correct permissions, and assigning the role, I was able to connect successfully with the following snippet:

HttpRequest req = new HttpRequest();
req.setMethod('POST');

req.setHeader('Authorization', 'Basic ' +
  EncodingUtil.base64Encode(Blob.valueOf(clientId + ':' + clientSecret)));
req.setHeader('Host','https://login.mypurecloud.com/oauth');
req.setHeader('content-type', 'application/x-www-form-urlencoded');
req.setEndpoint('https://login.mypurecloud.com/oauth/token');

req.setBody('grant_type=client_credentials');

Http http = new Http();

try{
    HTTPResponse res = http.send(req);

} catch(System.CalloutException e){
    // do something useful here
}

Of course, the tough part comes after, shuffling data back and forth and actually accomplishing something useful, but a connection's a start, right? Thanks again for your help, https://salesforce.stackexchange.com/users/2984/sfdcfox (yes, again with the invalid formatting)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.