我正在使用instagram API。在getUserID函数中,我试图获取经过身份验证的用户的userID。但是,当我尝试调用getUserID函数时,它不返回用户ID,而是说Array([meta] => Array([code] => 400 [error_type] => OAuthParameterException [error_message] =>缺少client_id或access_token URL参数))
// Variables
define("imageDirectory", 'pics/');
$clientID = 'xxxxxxxx';
$clientSecret = 'xxxxxxxxx';
$redirectURL = 'xxxxxxxxx';
//connect to insta
function connectToInstagram($url){
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Get Instagram User ID
function getUserID($userName){
$url = "https://api.instagram.com/v1/users/search?q=".$userName."&client_id=".$clientID;
$instagramInfo = connectToInstagram($url);
$results = json_decode($instagramInfo, true);
print_r($results);
// return $results['data'][0]['id'];
}
if ($_GET['code']) {
//Logged in
$code = $_GET['code'];
$url = "https://api.instagram.com/oauth/access_token";
$access_token_settings = array(
"client_id" => $clientID,
"client_secret" => $clientSecret,
"grant_type" => 'authorization_code',
"redirect_uri" => $redirectURL,
"code" => $code
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $access_token_settings);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$results = json_decode($result, true);
// print_r($result);
$userName = $results['user']['username'];
echo $userName;
getUserID($userName);
// getUserID($userName);
// echo $results;
}else{
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<a href="https://api.instagram.com/oauth/authorize/?client_id=<?php echo $clientID;?>&redirect_uri=<?php echo $redirectURL?>&response_type=code">Login</a>
</body>
</html>
<?php
}
它应该返回经过身份验证的用户的用户ID