太老了,但我希望对某些人有所帮助。
创建订阅需要 4 个步骤:-
第一步:将您的用户引导至我们的授权 URL:-
GET 请求:- https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code
第二步:接收来自 Instagram 的重定向
作为对第 1 步的回应,Instagram 将在成功时为您提供 http://your-redirect-uri?code=CODE,您将在第 3 步中使用它。
注意:CODE 不是访问令牌,您将使用 CODE 来获取访问令牌。
第三步:请求access_token:-
POST CURL 请求:-
curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
-F 'code=CODE' \
https://api.instagram.com/oauth/access_token
成功DEMO数据
{
"access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
"user": {
"id": "1574083",
"username": "snoopdogg",
"full_name": "Snoop Dogg",
"profile_picture": "..."
}
}
第四步:创建订阅
第四步有一些子步骤。
i) 向 Instagram API 发布 curl 请求
curl -F 'client_id=CLIENT-ID' \
-F 'client_secret=CLIENT-SECRET' \
-F 'object=user' \
-F 'aspect=media' \
-F 'verify_token=myVerifyToken' \
-F 'callback_url=http://YOUR-CALLBACK/URL' \
https://api.instagram.com/v1/subscriptions/
Note: myVerifyToken should be a access token of any one user, subscription is not created separately for every user, one subscription will be working for all the authenticated user of this app. so you may manually provide one. You do not create subscription again and again, so do not make calls to create subscription, when ever you think you need one subscription then only create one or usually you will continue with one or delete and recreate one.
ii) 成功后 Instagram 将提供
`https://your-callback.com/url/?hub.mode=subscribe&hub.challenge=15f7d1a91c1f40f8a748fd134752feb3&hub.verify_token=myVerifyToken` of which the callback page ( `http://YOUR-CALLBACK/URL` ) should only display `hub.challenge` that is:-
在回调页面例如:callback.php
<?php echo $_GET['hub_challenge']; //yes undescore in palce of dot. ?>
iii) 如果 Instagram API 将得到$_GET['hub_challenge'] 即15f7d1a91c1f40f8a748fd134752feb3,它将回复我们的帖子请求以创建订阅
类似
{
"meta": {
"code": 200
},
"data": [
{
"id": "1",
"type": "subscribe",
"object": "user",
"aspect": "media",
"callback_url": "https://your-callback.com/url/"
}
]
}
iii) 如果成功,您可以通过 GET 请求列出订阅,该请求可能直接来自您的浏览器。
GET 请求:- https://api.instagram.com/v1/subscriptions?client_secret=CLIENT-SECRET&client_id=CLIENT-ID
现在,当经过身份验证的用户发布回调页面时,回调页面将收到来自 Instagram api 的 GET 请求,其中包含一些包含 instagram user_id 的 JSON 数据,您将获得作为 object_id 和 media_id 的 post id。
你可以抓住它并使用下面的代码,是的,你可以使用比我更好的代码,这很棒。
$content = file_get_contents('php://input');
try {
if ($content === false) {
// Handle the error
//echo 'Whoops! Something went wrong!';
file_put_contents('subscriptions.log', 'getting empty content', FILE_APPEND);
} else {
$content_object = json_decode($content)[0];
$error = json_last_error();
file_put_contents('subscriptions.log', $error, FILE_APPEND);
$ig_id = $content_object->object_id;
$media_id = $content_object->data->media_id;
}
} catch (Exception $e) {
// Handle exception
//echo 'Whoops! Wrongly encoded data receiving!';
file_put_contents('subscriptions.log', $e->getMessage(), FILE_APPEND);
}