Last active
November 22, 2017 20:45
-
-
Save tristansokol/943d84e5036c4c34a42b019b821e3d5a to your computer and use it in GitHub Desktop.
php oauth example
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
<pre> | |
<?php | |
$client_id = 'sq0idp-xxx'; | |
$client_secret = 'sq0csp--xxxx'; | |
$redirect_uri= "http://localhost:8080/callback.php"; | |
$authorization_code = $_GET['code']; | |
if(!$authorization_code){ | |
die('something went wrong!'); | |
} | |
$url = 'https://connect.squareup.com/oauth2/token'; | |
$data = array( | |
'client_id' => $client_id, | |
'client_secret' => $client_secret, | |
'redirect_uri' => $redirect_uri, | |
'code' => $authorization_code | |
); | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/json\r\n", | |
'method' => 'POST', | |
'content' => json_encode($data) | |
) | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
echo "Result from /oauth2/token:<br>"; | |
var_dump($result); | |
$access_token = json_decode($result)->access_token; | |
//Refresh the token | |
$url = "https://connect.squareup.com/oauth2/clients/$client_id/access-token/renew"; | |
$data = array( | |
'access_token' => $access_token | |
); | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/json\r\n". | |
"Authorization: Client $client_secret\r\n", | |
'method' => 'POST', | |
'content' => json_encode($data) | |
) | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
echo "Result from /oauth2/clients/$client_id/access-token/renew:"; | |
var_dump($result); | |
//Revoke the token | |
$url = "https://connect.squareup.com/oauth2/revoke"; | |
$data = array( | |
'access_token' => $access_token, | |
'client_id' => $client_id | |
); | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/json\r\n". | |
"Authorization: Client $client_secret\r\n", | |
'method' => 'POST', | |
'content' => json_encode($data) | |
) | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
echo "Result from /oauth2/revoke:"; | |
var_dump($result); | |
?> | |
<a href="https://connect.squareup.com/oauth2/authorize?client_id=<?php echo $client_id;?>">Authorize App</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment