Last active
November 10, 2015 02:01
-
-
Save AaronTraas/809b4d44e30d9523491e to your computer and use it in GitHub Desktop.
Instagram auth test
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Instagram Auth Test</title> | |
</head> | |
<body> | |
<h1>Instagram Auth Test</h1> | |
<?php | |
// Register your app here: http://instagram.com/developer/ | |
$client_id = 'YOUR_CLIENT_ID'; | |
$client_secret = 'YOUR_CLIENT_SECRET'; | |
$redirect_url = "YOUR_REDIRECT_URL"; | |
// if the code parameter has been sent, we retrieve the access_token | |
if(isset($_GET['code'])) | |
{ | |
$code = $_GET['code']; | |
$url = "https://api.instagram.com/oauth/access_token"; | |
$access_token_parameters = array( | |
'client_id' => $client_id, | |
'client_secret' => $client_secret, | |
'grant_type' => 'authorization_code', | |
'redirect_uri' => $redirect_url, | |
'code' => $code | |
); | |
$curl = curl_init( $url ); | |
curl_setopt( $curl, CURLOPT_POST, true ); | |
curl_setopt( $curl, CURLOPT_POSTFIELDS, $access_token_parameters ); | |
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false ); | |
$result = curl_exec( $curl ); | |
curl_close( $curl ); | |
$arr = json_decode( $result, true ); | |
if( isset( $arr['access_token'] ) ) | |
{ | |
echo 'logged in user: ' . $arr['user']['username']; | |
} | |
else | |
{ | |
echo 'Error: ' . $arr[error_message]; | |
} | |
} | |
else | |
{ | |
$oauth_url = "https://api.instagram.com/oauth/authorize/?client_id=$client_id&redirect_uri=$redirect_url&response_type=code"; | |
echo "<a href=\"$oauth_url\">Log in to Instagram</a>"; | |
} | |
?> | |
</body> | |
</html> |
if i need to just get json results can i do a print of $arr (sorry very new to php)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a very crude, single-source-file piece of code that tests authentication against the Instagram API. In order to run this, you must register your application here:
http://instagram.com/developer/
And update
$client_id
,$client_secret
, and$redirect_url
with the information provided by Instagram that is specific to your application.