Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created September 1, 2016 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstachl/69079e086277c849f7fcdec2fd497856 to your computer and use it in GitHub Desktop.
Save tstachl/69079e086277c849f7fcdec2fd497856 to your computer and use it in GitHub Desktop.
Working example of Desk.com OAuth flow with PHP.
<?php
$req_url = 'https://YOURSITE.desk.com/oauth/request_token';
$authurl = 'https://YOURSITE.desk.com/oauth/authorize';
$acc_url = 'https://YOURSITE.desk.com/oauth/access_token';
$api_url = 'https://YOURSITE.desk.com/api/v2';
$conskey = 'CONSUMER_KEY';
$conssec = 'CONSUMER_SECRET';
$cbk_url = 'http://localhost:3000';
session_start();
// In state=1 the next request should include an oauth_token.
// If it doesn't go back to 0
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;
try {
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
if(!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$request_token_info = $oauth->getRequestToken($req_url, $cbk_url);
$_SESSION['secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
exit;
} else if($_SESSION['state']==1) {
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_url);
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
}
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
$oauth->fetch("$api_url/users/me.json");
$json = json_decode($oauth->getLastResponse());
print_r($json);
} catch(OAuthException $E) {
print_r($E);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment