【发布时间】:2018-12-28 05:55:48
【问题描述】:
问题
我在朋友的 WordPress 网站上构建了一个 Instagram 动态,以显示来自指定动态的八张最新图像。
我点击了这个端点来实现这一点:
https://api.instagram.com/v1/users/{$ig_user_id}/media/recent/?access_token={$ig_access_token}
到目前为止,我一直使用自己的测试 instagram 客户端来填充提要。
我们现在正准备上线,我尝试用我朋友的 Instagram 帐户替换 Instagram 用户 ID 和访问令牌。虽然我仍然可以生成访问令牌 (using this tool @ PixelUnion),但我无法再找到访问用户 ID 的方法。
这里有一个现有的 SO 线程和解决方案,它似乎不再有效:
Instagram how to get my user id from username?
问题
由于 Instagram API 受到明显限制,如何查找 Instagram 帐户用户 ID? (~ 2018 年中)
我的 Instagram 源代码 (PHP)
<?php
function prettify_instagram_image($image_data) {
// Plucks just what we need from the data returned from the Instagram API
$pretty_data= (object)[
"id" => $image_data->id ? $image_data->id : false,
"src_url" => $image_data->images->standard_resolution->url ? $image_data->images->standard_resolution->url : false,
"ig_link" => $image_data->link ? $image_data->link : false,
"likes" => $image_data->likes->count ? $image_data->likes->count : false,
"comments" => $image_data->comments->count ? $image_data->comments->count : false
];
return $pretty_data;
}
$ig_user_id = get_field('instagram_user_id', 'option'); // Pulling IG account info from WP Admin Options
$ig_access_token = get_field('instagram_access_token', 'option');
$ig_api_root = 'https://api.instagram.com/v1/users/';
$ig_api_method = '/media/recent/';
$images_to_display = 8;
// https://api.instagram.com/v1/users/{user-id}/media/recent/?access_token=ACCESS-TOKEN
// Instagram API connection
$response = wp_remote_get( $ig_api_root . $ig_user_id . $ig_api_method . "?access_token=" . $ig_access_token );
// Instagram response is JSON encoded, let's convert it to an object
$instagram_response = !is_wp_error($response) ? json_decode( $response['body'] ) : false;
$instagram_images = ($instagram_response && $instagram_response->data) ? array_map("prettify_instagram_image", $instagram_response->data) : false;
?>
【问题讨论】:
-
我也受此政策困扰,但我想出了一个不是
legal的方法,我只能提供一些提示。 Instagram 本身需要一个 api 来生成页面,因此您可以假装来自网站浏览器,使用他们的 api ,这需要使用 cookie 和 md5 散列密钥(隐藏在他们的 js 文件中)传递正确的标头。我只为公共页面做,因此不需要登录和获取私人代码,但你需要。然后在从api返回的json中,就可以找到你想要的了。艰难,但祝你好运。