Hello community,
I have a small problem related to retrieving the access token using REST API. What is weird is that although everything works fine in the local environment, the story changes when using the same code on the testing environment. In WordPress I have this small piece of code:
$resourceUrl = $this->identityUrl.'oauth/token?grant_type=client_credentials&client_id=' . $this->clientId . '&client_secret=' . $this->clientSecret;
$args = array(
'headers' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json'
)
);
$response = wp_remote_get($resourceUrl, $args);
The problem here is that although on local server everything works ok, and I can retrieve the authentication token, on the testing server, this does not work, the resulting decoded json of the response being this:
["headers"]=>
object(Requests_Utility_CaseInsensitiveDictionary)#1158 (1) {
["data":protected]=>
array(4) {
["server"]=>
string(5) "nginx"
["date"]=>
string(29) "Mon, 03 Sep 2018 09:18:57 GMT"
["content-type"]=>
string(30) "application/json;charset=UTF-8"
["content-length"]=>
string(3) "113"
}
}
["body"]=>
string(113) "{"requestId":"e097#1659eba5b83","success":false,"errors":[{"code":"600","message":"Access token not specified"}]}"
["response"]=>
array(2) {
["code"]=>
int(200)
["message"]=>
string(2) "OK"
}
["cookies"]=>
array(0) {
}
["filename"]=>
NULL
["http_response"]=>
object(WP_HTTP_Requests_Response)#1167 (5) {
["response":protected]=>
object(Requests_Response)#1155 (10) {
["body"]=>
string(113) "{"requestId":"e097#1659eba5b83","success":false,"errors":[{"code":"600","message":"Access token not specified"}]}"
["raw"]=>
string(270) "HTTP/1.1 200 OK
Server: nginx
Date: Mon, 03 Sep 2018 09:18:57 GMT
Content-Type: application/json;charset=UTF-8
Content-Length: 113
Connection: close
What I find weird is that it says that no access token was specified, even though that is exactly what I required to receive. Is there something I'm missing here? Thank you.
Solved! Go to Solution.
A good thing to unlearn: don't provide code if the question is truly about an HTTP/JSON API.
To prove there's an actual issue with the HTTP API, take all PHP code out of the equation as that code has no actual bearing on the API's operation (and, of course, the code you've shown is a tiny portion of the code actually being run since you're using a PHP HTTP library that isn't shown, nor is the code in which the instance $this->variables are being set, nor is the configuration file from which the client ID, client secret, and base URL are being read).
Removing PHP means using a debugging proxy server, or using an interactive HTTP client like cURL or Postman. Or just dumping the JSON request/response to the screen. Anything is better than confusing a PHP problem for an API problem!
Since your results are different across two environments and you say the code is the same, then the error is surely in your environment config. You may have the $this->identityUrl set incorrectly, for example (of course you haven't provided that value, so it's impossible to know). If it's accidentally set to
https://{{Marketo Munchkin ID}}.mktorest.com/rest/
instead of
https://{{Marketo Munchkin ID}}.mktorest.com/identity/
you'll get the error shown in your post.
A good thing to unlearn: don't provide code if the question is truly about an HTTP/JSON API.
To prove there's an actual issue with the HTTP API, take all PHP code out of the equation as that code has no actual bearing on the API's operation (and, of course, the code you've shown is a tiny portion of the code actually being run since you're using a PHP HTTP library that isn't shown, nor is the code in which the instance $this->variables are being set, nor is the configuration file from which the client ID, client secret, and base URL are being read).
Removing PHP means using a debugging proxy server, or using an interactive HTTP client like cURL or Postman. Or just dumping the JSON request/response to the screen. Anything is better than confusing a PHP problem for an API problem!
Since your results are different across two environments and you say the code is the same, then the error is surely in your environment config. You may have the $this->identityUrl set incorrectly, for example (of course you haven't provided that value, so it's impossible to know). If it's accidentally set to
https://{{Marketo Munchkin ID}}.mktorest.com/rest/
instead of
https://{{Marketo Munchkin ID}}.mktorest.com/identity/
you'll get the error shown in your post.