I am a beginner trying to figure out how this API and oAuth works, and it seems like every tutorial / reference I find whether on this API or ANY API assumes a level of knowledge that I can’t fathom because I can’t find information/reference to get me started. I thought I would try to use the Asana API to pull a list of my projects on a page on my wordpress website. Is there a way to do it without the client library? Just a simple php/curl based request?
I tried to use the library first, but don’t know how to install/implement because they mention something about composer, and environment variables that I don’t know how to set, etc… see what I mean by there being no beginner info that I can find?
Here’s what I’ve done:
- Create a Wordpress page called Asana Auth on the website, url: MYDOMAIN/asana-auth/
- Create an app in Asana with a client ID, secret, redirect url (MYDOMAIN/asana-projects/
- On the Asana Auth page, I created a button that when clicked will send the auth information to the Asana authentication uri. My button code:
<a class="custom-button" href="https://app.asana.com/-/oauth_authorize?response_type=code&client_id=MY_ID&redirect_uri=https%3A%2F%2FMY_DOMAIN.com%2Fasana-projects%2F&state=drickstate" title="Auth">Authenticate</a>
I click that button, and I can see a successful response in the url with my code included as a parameter. I grab the code using $_GET, and try to exchange it for a token using the following:
<?php
$client_id = 'MY_ID';
$client_secret = 'MY_SECRET';
$redirect_uri = 'https%3A%2F%2FMY_DOMAIN%2Fasana-projects%2F&drickstate';
$access_code = $_GET['code'];
$token_uri = 'https://app.asana.com/-/oauth_token';
$post_fields = array(
'grant_type' => 'authorization_code',
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'code' => $access_code
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_uri);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") {
return $server_output;
} else {
echo 'something went wrong';
}
?>
It’s saying something went wrong of course, and I’m sure it’s because I don’t have enough information to properly setup the token exchange. I am basing my post request on the Asana API Authentication page. Can someone please either explain if this is possible without the client library, or point me to some reference / tutorials / guides that can get me started? I’ve built dozens of sites with PHP and wordpress, but this is a whole new ballgame and I’m INSANELY frustrated that I can’t find a find beginner references.
I very much appreciate anyone’s helpful response.