【问题标题】:X-Insta-Forwarded-For Error Instagram API using PHP cURLX-Insta-Forwarded-For 错误 Instagram API 使用 PHP cURL
【发布时间】:2023-04-04 03:47:02
【问题描述】:

我正在尝试使用此 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 发布到 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?

【问题讨论】:

  • “仅用于学术目的” 没关系,如果它是非法的,它不属于 SO。

标签: javascript php curl instagram instagram-api


【解决方案1】:

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

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

如果选中Enforce signed header,则您无法在没有X-Insta-Forwarded-For 的情况下向Instagram API 发布任何内容。就像如果选中了“Enforce signed requests”,则没有 sig 参数就无法向 API 发出请求。

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

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 2013-10-22
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多