Created
March 17, 2013 19:11
-
-
Save kenmickles/5183114 to your computer and use it in GitHub Desktop.
A simple PHP implementation of Foursquare's OAuth 2.0 flow. Useful for quickly getting a test app up and running.
This file contains hidden or 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 | |
/** | |
* This is a quick and dirty implementation of Foursquare's OAuth 2.0 flow. It handles the | |
* auth redirect and code exchange, and spits out a user access token. | |
* | |
* See https://developer.foursquare.com/overview/auth.html for more info. | |
* | |
* @author Ken Mickles | |
*/ | |
// copy these from your Foursquare app settings (https://foursquare.com/developers/apps) | |
$client_id = ''; | |
$client_secret = ''; | |
// make sure this URL is set as a redirect URI | |
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; | |
// if we've got a code, the user just came back from authorizing the app | |
// next, we exchange the code for an access token | |
if ( isset($_GET['code']) ) { | |
$url = 'https://foursquare.com/oauth2/access_token?client_id=' . $client_id . '&client_secret=' . $client_secret . '&grant_type=authorization_code&redirect_uri=' . $redirect_uri . '&code=' . $_GET['code']; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
$data = json_decode($result, 1); | |
if ( isset($data['access_token']) ) { | |
echo "Your access token is: <b>{$data['access_token']}</b>"; | |
} | |
else { | |
echo "Something went wrong: <pre>{$result}</pre>"; | |
} | |
} | |
// redirect to the Foursquare auth dialog | |
else { | |
header('Location: https://foursquare.com/oauth2/authenticate?client_id=' . $client_id . '&response_type=code&redirect_uri=' . $redirect_uri); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment