我正在尝试创建一个网站,我可以在其中关注或取消关注我的人。我已经创建了我的网站,我从 PHP Instagram API中获取了令牌,但在我获得令牌后,我无法关注或取消关注 PHP 中的人员。所以我尝试在 JavaScript 上发布帖子请求,但它没有工作,然后我在 jQuery 中尝试相同的发送帖子,但我总是收到错误我不喜欢#39;不知道为什么。
所以这是我从 PHP :
获取令牌后的代码<?php
session_start();
echo 'Your token is: ' . $_SESSION["token4"];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>API Instagram</title>
<style>
#log{
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$( document ).ready(function() {
var TOKEN = "<?php echo $_SESSION["token4"] ?>";
var USERID = "1574083";
var url = "https://api.instagram.com/v1/users/"+USERID+"/relationship?access_token="+TOKEN;
$( "#g" ).append( "<strong>"+url+"</strong>" );
$( "#follow" ).click(function() {
var request = $.ajax({
url: url,
method: "POST",
data: { action : "follow" },
dataType: "html"
});
request.done(function( msg ) {
$( "#log" ).html( msg );
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
});
});
</script>
</head>
<body>
<br>
<p id="g"></p>
<p id="log"></p>
<button id="follow">Follow</button>
</body>
</html>
这就是我从Instagram API v2获取令牌的方法,但它不起作用我也会在我的脚本上隐藏API密钥
<?php
session_start();
if (!empty($_SESSION["token4"])) {
header("Location: next.php");
}
require 'Instagram.php';
use MetzWeb\Instagram\Instagram;
// initialize class
$instagram = new Instagram(array(
'apiKey' => '123456789123456789',
'apiSecret' => '123456789123456789',
'apiCallback' => 'http://localhost:3000/' // must point to success.php
));
// create login URL
$login = $instagram->getLoginUrl(array(
'basic',
'likes',
'comments',
'relationships'
));
# code...
echo "<a href='https://www.instagram.com/oauth/authorize?client_id=51fb9664753d49b9985e0c69f9a20dcb&redirect_uri=http://localhost:3000/&response_type=code&scope=basic+public_content+follower_list+comments+relationships+likes'>Login with Instagram</a>";
$code = $_GET['code'];
if (!empty($_GET['code'])) {
$token = $instagram->getOAuthToken($code, true);
$instagram->setAccessToken($token);
$data = $instagram->getOAuthToken($code);
$_SESSION["token4"] = $token;
echo "<br><br><a href='next.php'>NEXT</a>";
}
?>