PHP Readmill oAuth Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please see the blog for more detailed information about this gist: http://www.johnstonianera.com/readmill-api-authentication-with-oauth-using-php/