X-Insta-Forwarded-For Error Instagram API 使用 PHP cURL

X-Insta-Forwarded-For Error Instagram API using PHP cURL

我正在尝试使用此 api 和 POST 关系方法制作一个 post 到 Instagram。

这是我正在使用的端服务器代码:

<?php

$url = "https://api.instagram.com/v1/users/<user>/relationship";
$ips= (isset($_SERVER['SERVER_ADDR'])) ? $_SERVER['SERVER_ADDR'] : gethostbyname(gethostname());
$signature = (hash_hmac('sha256', $ips, '<secret>', false));

$join = join('|', array($ips, $signature));

$headerData = array('Accept: application/json');

$headerData[] = 'X-Insta-Forwarded-For: ' .$join;

$fields = array(
        'access_token'       =>      '<access_token>',
        'action'             =>      'follow'
    );

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headerData);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

// grab URL and pass it to the browser
$result = curl_exec($ch);
$error = curl_error($ch);
// close cURL resource, and free up system resources
curl_close($ch);

print_r($result);
?>

如您所见,我已将此行注释掉:

//curl_setopt($ch, CURLOPT_HTTPHEADER, $headerData);

因为据我所知,要使用 cURL POST 到 Instagram,我只需要一个有效的 access_token,但 Instagram 返回给我这个错误:

{"code": 403, "error_type": "OAuthForbiddenException", "error_message": "Invalid header: X-Insta-Forwarded-For is required for this operation"}

我明白这意味着什么,但我的问题是,是否有人在没有在 Instagram API 上注册应用程序并使用在互联网上找到的外部 access_token 的情况下尝试过类似的事情?

您正在使用的 access_token 属于可能具有此签名 header POST 限制的应用程序(“强制签名 header").

转到 https://instagram.com/developer/clients/manage/ 创建或管理您的应用程序,单击 编辑 应用程序,然后单击 安全性 选项卡。你应该看到下面的页面。 Enforce signed requests(新的更好的签名方法)和Enforce signed header(旧方法,将于 9 月弃用) 1) 是避免盗用的方法access_token。

没有X-Insta-Forwarded-For如果强制签名[=46=,你不能post任何东西到Instagram API ] 已勾选。就像你不能在没有 sig 参数的情况下向 API 发出请求一样,如果选中“强制签名请求”。

我推荐你学习 new signed request method observing those Instagram API secure requests considerations