Instagram订阅API要求访问令牌 [英] Instagram Subscription API Asking For Access Token

查看:159
本文介绍了Instagram订阅API要求访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Instagram订阅API订阅Instagram实时更新.我已成功在Instagram上订阅了多个订阅.但是现在当我尝试订阅时,它给了我以下错误:

I have been using Instagram Subscription API to subscribe to Instagram real time updates. I have successfully subscribed to multiple subscriptions on Instagram. But now it is giving me the following error when I try to subscribe:

meta": {
    "error_type": "OAuthAccessTokenException",
    "code": 400,
    "error_message": "The access_token provided is invalid."
}

更早以前,它从未用于请求订阅API的访问令牌.谁能解释一下Instagram API.

Earlier it never used to ask for access token for subscription API. Can anyone please explain Instagram API.

推荐答案

年龄太大了,但希望对某些人有所帮助.

Too old but I hope will be helpful to some people.

创建订阅的过程分为4个步骤:-

第一步:将您的用户定向到我们的授权URL:-

Step One: Direct your user to our authorization URL:-

获取请求:-https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

第二步:从Instagram接收重定向

Step Two: Receive the redirect from Instagram

作为第1步的回应,Instagram将为您提供 http://your-redirect-uri ?code = CODE 成功,您将在第三步中使用它. 注意:CODE不是访问令牌,您将使用CODE获取访问令牌.

As a response of step 1, Instagram will provide you a http://your-redirect-uri?code=CODE on success, which you will use in step three. Note: CODE is not access token, you will use CODE to get access token.

第三步:请求访问令牌:-

Step Three: Request the access_token:-

POST CURL请求:-

POST CURL Request :-

 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)将curl请求发布到Instagram API

Step Four has some sub steps. i) POST curl request to Instagram API

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将提供

ii) On success Instagram will provide

   `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

on callback page eg: callback.php

<?php echo $_GET['hub_challenge']; //yes undescore in palce of dot.  ?>

iii)如果Instagram API将获得$_GET['hub_challenge']15f7d1a91c1f40f8a748fd134752feb3,它将在

iii) If the Instagram API will get $_GET['hub_challenge'] that is 15f7d1a91c1f40f8a748fd134752feb3 here it will reply for our post request to create a subscription with

类似

{
    "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

iii) If success you can list out the subscription with a GET request may be directly from your browser. GET Request:- 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的形式获得该ID,即发布ID. 您可以使用以下代码捕获并使用它,是的,可以使用比我更好的代码,那就是伟大的代码.

now when ever the authenticated users will post the callback page will get a GET request from Instagram api with some JSON data containing instagram user_id that you will get as object_id and media_id that is the post id. You can catch that and use that with the below code, yes you can use better code than me , that is GREAT.

 $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);
}

这篇关于Instagram订阅API要求访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆