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

时间:2015-06-02 09:57:39

标签: javascript php curl instagram instagram-api

我试图使用这个api和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而尝试了类似的东西吗?

1 个答案:

答案 0 :(得分:1)

您正在使用的access_token属于可能具有此签名标头POST限制的应用程序(“强制签名标题”)。

转到https://instagram.com/developer/clients/manage/创建或管理您的应用,点击修改应用,然后点击安全标签。你应该看到下面的页面。 执行签名请求(新的更好的签名方法)和强制签名的标题(旧的,将于9月1日弃用)是避免滥用被盗的方法的access_token。

如果选中强制签名标题,则无法在没有 X-Insta-Forwarded-For 的情况下向Instagram API发布任何内容。正如在没有 sig 参数的情况下,如果选中“强制执行签名请求”,则无法向API发出请求。

我建议你学习new signed request method观察那些Instagram API secure requests considerations

Instagram App Security Tab