Help getting started with API using PHP and curl
I'm a relative novice dealing with APIs using PHP. I've done a fair amount of PHP programming, but API interaction is new to me.
I'm using a permanent access token since only I will be using this "app."
I am attempting to authorize, but I'm getting: Access token is unknown or invalid
From the docs, I take this to mean I am using the wrong URL. The docs say you get the host (url) through the process of authorizing via OAuth, but if I'm not using OAuth, how do I know the url? I'm using https://www.wrike.com/api/v4" since that's what all the examples I find use, but why am I getting the error?
Here's the function I'm using to call the API ($method is 'GET', for example, $path_ext is '/folders', for example):
function wrike_CallAPI($method, $path_ext, $data = array())
{
$url = _WRIKEAPIPATH_.$path_ext;
$curl = curl_init();
if(!string_test($data)){
$data = false;
}
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "GET":
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'GET' );
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Authentication:
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: bearer '._WRIKEPERMACCESSTOKEN_));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
I assume I am fouling something up in the PHP here. Any help is appreciated!
Thanks!
Matt
UPDATE: I get the same result using Postman with that url and my permanent access token. What am I missing?
Welcome to the Community Matt Tyree 👋
I can see that you've discussed this with our Support specialists already 👍
Lisa Community Team at Wrike Wrike Product Manager Become a Wrike expert with Wrike Discover
Lisa Wrike Team member Become a Wrike expert with Wrike Discover
Indeed. I was informed as to how to detect the proper url when using a permanent access token. I needed to be using app-us2.wrike.com instead of www.wrike.com.
Thank you for posting the solution here Matt Tyree 🤗
Lisa Community Team at Wrike Wrike Product Manager Become a Wrike expert with Wrike Discover
Lisa Wrike Team member Become a Wrike expert with Wrike Discover