Skip to content

Instantly share code, notes, and snippets.

@johnstonian
Created December 4, 2012 15:06
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save johnstonian/4204947 to your computer and use it in GitHub Desktop.
PHP Readmill oAuth Methods
<?php
function get_access_token($authCode) {
$url = "https://readmill.com/oauth/token.json";
$clientId = "YOUR CLIENT ID";
$clientSecret = "YOUR CLIENT SECRET";
$redirectURI = "YOUR REDIRECT URI";
// setup variables to post
$post_fields .= "grant_type=authorization_code";
$post_fields .= "&client_id=".$clientId;
$post_fields .= "&client_secret=".$clientSecret;
$post_fields .= "&redirect_uri=".$redirectURI;
$post_fields .= "&code=" . $authCode;
// create curl resource
$ch = curl_init();
// set the url, enable POST return data, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,5);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_fields);
// execute curl call
$output = curl_exec($ch);
curl_close($ch);
// convert returned curl response to json
$oAuthObj = json_decode($output);
return $oAuthObj->access_token;
}
function api_request($url) {
$ch = curl_init();
// set the url, enable return data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// execute curl call
$output = curl_exec($ch);
curl_close($ch);
$requestedObj = json_decode($output);
return $requestedObj;
}
// get authorization code sent from the readmill oauth page
$authCode = $_GET['code'];
// set access token from readmill
$access_token = get_access_token($authCode);
// make an API call to get user info
$userURL = "https://api.readmill.com/v2/me?access_token=".$access_token;
$userObj = api_request($userURL);
$userObj = $userObj->user;
// print user object
echo '<h2>USER Object</h2>';
print_r($userObj);
?>
@johnstonian
Copy link
Author

Please see the blog for more detailed information about this gist: http://www.johnstonianera.com/readmill-api-authentication-with-oauth-using-php/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment